Tap here to switch tabs
Problem
Submissions

Problem: Number of Subarrays That Match a Pattern II

Number of Subarrays That Match a Pattern II asks you to count contiguous subarrays of the pattern's required length, where consecutive values follow increases, equalities, and decreases.

Statement

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

A contiguous subarray of nums of size m+1m + 1 starting at index i is said to match pattern if for every integer 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 total number of subarrays in nums that match pattern.

Constraints:

  • 2≤n==2 \leq n == nums.length ≤106\leq 10^6

  • 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 II

Number of Subarrays That Match a Pattern II asks you to count contiguous subarrays of the pattern's required length, where consecutive values follow increases, equalities, and decreases.

Statement

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

A contiguous subarray of nums of size m+1m + 1 starting at index i is said to match pattern if for every integer 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 total number of subarrays in nums that match pattern.

Constraints:

  • 2≤n==2 \leq n == nums.length ≤106\leq 10^6

  • 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