Tap here to switch tabs
Problem
Submissions

Problem: Number of Subarrays That Match a Pattern I

Number of Subarrays That Match a Pattern I counts subarrays one element longer than a given pattern when every adjacent pair follows its required increase, decrease, or equality, returning the total.

Statement

You are given an integer array nums of length n, and an integer array pattern of length m containing only the integers −1-1, 00, and 11.

A subarray of nums of size m+1m + 1 starting at index i matches pattern if for every index k where 0≤k<m0 \leq k < m, the following conditions hold:

  • nums[i + k + 1] > nums[i + k] if pattern[k] == 1

  • nums[i + k + 1] == nums[i + k] if pattern[k] == 0

  • nums[i + k + 1] < nums[i + k] if pattern[k] == -1

Return the number of subarrays of nums that match pattern.

Constraints:

  • 2≤n==2 \leq n == nums.length ≤100\leq 100

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • 1≤m==1 \leq m == pattern.length <n< n

  • −1≤-1 \leq pattern[i] ≤1\leq 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Number of Subarrays That Match a Pattern I

Number of Subarrays That Match a Pattern I counts subarrays one element longer than a given pattern when every adjacent pair follows its required increase, decrease, or equality, returning the total.

Statement

You are given an integer array nums of length n, and an integer array pattern of length m containing only the integers −1-1, 00, and 11.

A subarray of nums of size m+1m + 1 starting at index i matches pattern if for every index k where 0≤k<m0 \leq k < m, the following conditions hold:

  • nums[i + k + 1] > nums[i + k] if pattern[k] == 1

  • nums[i + k + 1] == nums[i + k] if pattern[k] == 0

  • nums[i + k + 1] < nums[i + k] if pattern[k] == -1

Return the number of subarrays of nums that match pattern.

Constraints:

  • 2≤n==2 \leq n == nums.length ≤100\leq 100

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • 1≤m==1 \leq m == pattern.length <n< n

  • −1≤-1 \leq pattern[i] ≤1\leq 1