Input: nums =[1,2,3,4,5,6], pattern =[1,1]Output: 4Explanation: 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.Hence, there are 4 subarrays in nums that match the pattern.
Input: nums =[1,4,4,1,3,5,5,3], pattern =[1,0,-1]Output: 2Explanation: 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.Hence, there are 2 subarrays in nums that match the pattern.
The key idea is to check every subarray of length m+1 in nums and compare the difference pattern with the given pattern. If all differences match the pattern, we count it. This works because the constraints are small, so a brute-force sliding window is efficient.
classSolution {
public:int countMatchingSubarrays(vector<int>& nums, vector<int>& pattern) {
int n = nums.size(), m = pattern.size(), ans =0;
for (int i =0; i + m < n; ++i) {
bool ok = true;
for (int k =0; k < m; ++k) {
if ((pattern[k] ==1&& nums[i+k+1] <= nums[i+k]) || (pattern[k] ==0&& nums[i+k+1] != nums[i+k]) || (pattern[k] ==-1&& nums[i+k+1] >= nums[i+k])) {
ok = false;
break;
}
}
if (ok) ++ans;
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
funccountMatchingSubarrays(nums []int, pattern []int) int {
n, m, ans:= len(nums), len(pattern), 0fori:=0; i+m < n; i++ {
ok:=truefork:=0; k < m; k++ {
if (pattern[k] ==1&&nums[i+k+1] <=nums[i+k]) || (pattern[k] ==0&&nums[i+k+1] !=nums[i+k]) || (pattern[k] ==-1&&nums[i+k+1] >=nums[i+k]) {
ok = falsebreak }
}
ifok {
ans++ }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintcountMatchingSubarrays(int[] nums, int[] pattern) {
int n = nums.length, m = pattern.length, ans = 0;
for (int i = 0; i + m < n; ++i) {
boolean ok =true;
for (int k = 0; k < m; ++k) {
if ((pattern[k]== 1 && nums[i+k+1]<= nums[i+k]) || (pattern[k]== 0 && nums[i+k+1]!= nums[i+k]) || (pattern[k]==-1 && nums[i+k+1]>= nums[i+k])) {
ok =false;
break;
}
}
if (ok) ++ans;
}
return ans;
}
}
classSolution {
funcountMatchingSubarrays(nums: IntArray, pattern: IntArray): Int {
val n = nums.size
val m = pattern.size
var ans = 0for (i in0..(n - m - 1)) {
var ok = truefor (k in0 until m) {
if ((pattern[k] ==1&& nums[i+k+1] <= nums[i+k]) || (pattern[k] ==0&& nums[i+k+1] != nums[i+k]) || (pattern[k] == -1&& nums[i+k+1] >= nums[i+k])) {
ok = falsebreak }
}
if (ok) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcountMatchingSubarrays(self, nums: list[int], pattern: list[int]) -> int:
n, m, ans = len(nums), len(pattern), 0for i in range(n - m):
ok =Truefor k in range(m):
if (pattern[k] ==1and nums[i+k+1] <= nums[i+k]) or \
(pattern[k] ==0and nums[i+k+1] != nums[i+k]) or \
(pattern[k] ==-1and nums[i+k+1] >= nums[i+k]):
ok =Falsebreakif ok:
ans +=1return ans
impl Solution {
pubfncount_matching_subarrays(nums: Vec<i32>, pattern: Vec<i32>) -> i32 {
let n = nums.len();
let m = pattern.len();
letmut ans =0;
for i in0..(n - m) {
letmut ok =true;
for k in0..m {
if (pattern[k] ==1&& nums[i+k+1] <= nums[i+k]) || (pattern[k] ==0&& nums[i+k+1] != nums[i+k]) || (pattern[k] ==-1&& nums[i+k+1] >= nums[i+k]) {
ok =false;
break;
}
}
if ok {
ans +=1;
}
}
ans
}
}