Count Subarrays Matching a Comparison Pattern
Problem statement
You are given an integer array numbers and an integer array pattern containing only -1, 0, and 1.
Each pattern value describes one adjacent comparison:
1means the next value is greater.0means the next value is equal.-1means the next value is smaller.
Return the number of contiguous subarrays of length pattern.length + 1 whose adjacent comparisons exactly match the complete pattern.
Function
countMatchingSubarrays(numbers: int[], pattern: int[]) → intExamples
Example 1
numbers = [1,2,3,4,5,6]pattern = [1,1]return = 4Every length-three window is strictly increasing, so all four candidate windows match.
Example 2
numbers = [1,4,4,1,3,5,5,3]pattern = [1,0,-1]return = 2The windows [1,4,4,1] and [3,5,5,3] increase, stay equal, and then decrease.
Constraints
2 <= numbers.length <= 10^51 <= pattern.length < numbers.length-10^9 <= numbers[i] <= 10^9pattern[i]is-1,0, or1.