From 946aa90eb2beeeccbd69b5be9cabddfc600c363c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D5=A1=C9=A8=D5=BC=C9=A2=D3=84=D5=A1=D6=85=D5=BC=C9=A2?= Date: Fri, 16 Feb 2024 00:27:26 +0800 Subject: [PATCH] 3034 - Number of Subarrays That Match a Pattern I (Medium) --- ...subarrays-that-match-a-pattern-i-medium.md | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 solutions/3000-3099/3034-number-of-subarrays-that-match-a-pattern-i-medium.md diff --git a/solutions/3000-3099/3034-number-of-subarrays-that-match-a-pattern-i-medium.md b/solutions/3000-3099/3034-number-of-subarrays-that-match-a-pattern-i-medium.md new file mode 100644 index 0000000000..e40e580ab5 --- /dev/null +++ b/solutions/3000-3099/3034-number-of-subarrays-that-match-a-pattern-i-medium.md @@ -0,0 +1,91 @@ +--- +description: 'Author: @wingkwong | https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/' +--- + +# 3034 - Number of Subarrays That Match a Pattern I (Medium) + +## Problem Link + +https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/ + +## Problem Statement + +You are given a **0-indexed** integer array `nums` of size `n`, and a **0-indexed** integer array `pattern` of size `m` consisting of integers `-1`, `0`, and `1`. + +A subarray `nums[i..j]` of size `m + 1` is said to match the `pattern` if the following conditions hold for each element `pattern[k]`: + +- `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**count** of subarrays in* `nums` *that match the* `pattern`. + +**Example 1:** + +``` +Input: nums = [1,2,3,4,5,6], pattern = [1,1] +Output: 4 +Explanation: 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. +``` + +**Example 2:** + +``` +Input: nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1] +Output: 2 +Explanation: 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. +``` + +**Constraints:** + +- $2 <= n == nums.length <= 100$ +- $1 <= nums[i] <= 10 ^ 9$ +- $1 <= m == pattern.length < n$ +- $-1 <= pattern[i] <= 1$ + +## Approach 1: Brute Force + +Since the constraints are small, we can just brute force it. The idea is to test all subarrays. We iterate in $nums$. For each starting point $i$ in $[0, n - m - 1]$ where $n$ is the size of $nums$ and $m$ is the size of $pattern$, we iterate each element in pattern to check the condition. If it isn't valid, then that means the entire subarray is not valid, so we can break it and try the next one, else we increase $ans$ by $1$ and move onto the next one. + +For larger constraints, one may use Z Algo or KMP to achieve $O(n)$. + +- Time Complexity: $O(n ^ 2)$ +- Space Complexity: $O(1)$ + + + + + +```cpp +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + int ans = 0, n = nums.size(), m = pattern.size(); + // we start from each `i` to check the subarray + // if the pattern size is `m`, + // the valid starting point is [0, n - m - 1] + for (int i = 0; i < n - m; i++) { + int ok = 1; + // iterate each element in `pattern` + for (int k = 0; k < m; k++) { + // just check those 3 cases defined the the problem statement + // if it cannot fulfil, then set `ok` to `0` then break the loop + if ((pattern[k] == 1 && !(nums[i + k] < nums[i + k + 1])) || + (pattern[k] == 0 && !(nums[i + k] == nums[i + k + 1])) || + (pattern[k] == -1 && !(nums[i + k] > nums[i + k + 1]))) { + ok = 0; + break; + } + } + // if `ok` is 1, that means the subarray [i, i + m - 1] matching the `pattern` + ans += ok; + } + return ans; + } +}; +``` + + + \ No newline at end of file