Skip to content

Practice Number of Subarrays That Match a Pattern I

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3034
LeetCode Problem

Number of Subarrays That Match a Pattern I

Problem Statement

Given nums and a pattern of length m containing only -1, 0, and 1, count contiguous subarrays of length m + 1 matching all m adjacent comparisons. A pattern value of 1 requires the next element to be larger, 0 requires equality, and -1 requires the next element to be smaller. Return the number of matching subarrays.

Source: Number of Subarrays That Match a Pattern I on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,3,4,5,6], pattern = [1,1]
Output
4

# Example 2

Input
nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
Output
2

Constraints

2 <= n == nums.length <= 100
1 <= nums[i] <= 10^9
1 <= m == pattern.length < n
-1 <= pattern[i] <= 1

Related LeetCode Problems

Practice more problems similar to Number of Subarrays That Match a Pattern I.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.