3034. Number of Subarrays That Match a Pattern I Medium

@problem@discussion
#Array#Rolling Hash#String Matching#Hash Function



1/**
2 * [3034] Number of Subarrays That Match a Pattern I
3 *
4 * You are given a 0-indexed integer array nums of size n, and a 0-indexed integer array pattern of size m consisting of integers -1, 0, and 1.
5 * A <span data-keyword="subarray">subarray</span> nums[i..j] of size m + 1 is said to match the pattern if the following conditions hold for each element pattern[k]:
6 * 
7 * 	nums[i + k + 1] > nums[i + k] if pattern[k] == 1.
8 * 	nums[i + k + 1] == nums[i + k] if pattern[k] == 0.
9 * 	nums[i + k + 1] < nums[i + k] if pattern[k] == -1.
10 * 
11 * Return the count of subarrays in nums that match the pattern.
12 *  
13 * <strong class="example">Example 1:
14 * 
15 * Input: nums = [1,2,3,4,5,6], pattern = [1,1]
16 * Output: 4
17 * Explanation: The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
18 * Hence, there are 4 subarrays in nums that match the pattern.
19 * 
20 * <strong class="example">Example 2:
21 * 
22 * Input: nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
23 * Output: 2
24 * Explanation: Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
25 * Hence, there are 2 subarrays in nums that match the pattern.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	2 <= n == nums.length <= 100
31 * 	1 <= nums[i] <= 10^9
32 * 	1 <= m == pattern.length < n
33 * 	-1 <= pattern[i] <= 1
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/
39// discuss: https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn count_matching_subarrays(nums: Vec<i32>, pattern: Vec<i32>) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3034() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.