From 6add7beaf1520f2204e9a65e5ef4b5c1ce69cce1 Mon Sep 17 00:00:00 2001 From: mankalavaishnavi Date: Mon, 17 Jun 2024 17:42:01 +0530 Subject: [PATCH] added 3034-lc-solution --- ...ber-of-Subarrays-That-Match-a-Pattern-I.md | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 dsa-solutions/lc-solutions/3000-3099/3034-Number-of-Subarrays-That-Match-a-Pattern-I.md diff --git a/dsa-solutions/lc-solutions/3000-3099/3034-Number-of-Subarrays-That-Match-a-Pattern-I.md b/dsa-solutions/lc-solutions/3000-3099/3034-Number-of-Subarrays-That-Match-a-Pattern-I.md new file mode 100644 index 000000000..97ec2615d --- /dev/null +++ b/dsa-solutions/lc-solutions/3000-3099/3034-Number-of-Subarrays-That-Match-a-Pattern-I.md @@ -0,0 +1,147 @@ +--- +id: number-of-subarrays-that-match-a-pattern-i +title: Number of Subarrays That Match a Pattern I (LeetCode) +sidebar_label: 3034-NumberOfSubarraysThatMatchAPatternI +tags: + - Array + - Pattern + - Sliding Window +description: Count the number of subarrays in an integer array that match a given pattern. +sidebar_position: 3034 +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [Number of Subarrays That Match a Pattern I](https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/) | [Number of Subarrays That Match a Pattern I Solution on LeetCode](https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | + +## Problem Description + +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 + +To solve this problem, we can use a sliding window approach to efficiently count the number of subarrays that match the given pattern. Here's the approach: + +1. Iterate through the array with a sliding window of size `m+1`. +2. For each window, check if the subarray matches the pattern. +3. Increment the count if the subarray matches the pattern. + +### Solution Code + +#### Python + +```python +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + n, m = len(nums), len(pattern) + count = 0 + + for i in range(n - m): + match = True + for k in range(m): + if (pattern[k] == 1 and nums[i + k + 1] <= nums[i + k]) or \ + (pattern[k] == 0 and nums[i + k + 1] != nums[i + k]) or \ + (pattern[k] == -1 and nums[i + k + 1] >= nums[i + k]): + match = False + break + if match: + count += 1 + + return count +``` + +#### C++ + +```c++ +#include +using namespace std; + +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + int n = nums.size(), m = pattern.size(); + int count = 0; + + for (int i = 0; i <= n - m - 1; ++i) { + bool match = true; + for (int k = 0; k < m; ++k) { + if ((pattern[k] == 1 && nums[i + k + 1] <= nums[i + k]) || + (pattern[k] == 0 && nums[i + k + 1] != nums[i + k]) || + (pattern[k] == -1 && nums[i + k + 1] >= nums[i + k])) { + match = false; + break; + } + } + if (match) { + count++; + } + } + + return count; + } +}; +``` + +#### Java + +```java +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + int n = nums.length, m = pattern.length; + int count = 0; + + for (int i = 0; i <= n - m - 1; ++i) { + boolean match = true; + for (int k = 0; k < m; ++k) { + if ((pattern[k] == 1 && nums[i + k + 1] <= nums[i + k]) || + (pattern[k] == 0 && nums[i + k + 1] != nums[i + k]) || + (pattern[k] == -1 && nums[i + k + 1] >= nums[i + k])) { + match = false; + break; + } + } + if (match) { + count++; + } + } + + return count; + } +} +``` + +### Conclusion +The solutions use a sliding window approach to efficiently count the number of subarrays that match +the given pattern. This ensures an efficient and straightforward way to solve the problem across +different programming languages. \ No newline at end of file