Skip to content

Commit 6947789

Browse files
authored
Improved task 3034
1 parent 63a38bd commit 6947789

File tree

1 file changed

+22
-19
lines changed
  • src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i

1 file changed

+22
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
1-
3033\. Modify the Matrix
1+
3034\. Number of Subarrays That Match a Pattern I
22

3-
Easy
3+
Medium
44

5-
Given a **0-indexed** `m x n` integer matrix `matrix`, create a new **0-indexed** matrix called `answer`. Make `answer` equal to `matrix`, then replace each element with the value `-1` with the **maximum** element in its respective column.
5+
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`.
66

7-
Return _the matrix_ `answer`.
7+
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]`:
8+
9+
* `nums[i + k + 1] > nums[i + k]` if `pattern[k] == 1`.
10+
* `nums[i + k + 1] == nums[i + k]` if `pattern[k] == 0`.
11+
* `nums[i + k + 1] < nums[i + k]` if `pattern[k] == -1`.
12+
13+
Return _the **count** of subarrays in_ `nums` _that match the_ `pattern`.
814

915
**Example 1:**
1016

11-
![](https://assets.leetcode.com/uploads/2023/12/24/matrix1.png)
17+
**Input:** nums = [1,2,3,4,5,6], pattern = [1,1]
1218

13-
**Input:** matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
19+
**Output:** 4
1420

15-
**Output:** [[1,2,9],[4,8,6],[7,8,9]]
21+
**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.
1622

17-
**Explanation:** The diagram above shows the elements that are changed (in blue).
18-
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8.
19-
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.
23+
Hence, there are 4 subarrays in nums that match the pattern.
2024

2125
**Example 2:**
2226

23-
![](https://assets.leetcode.com/uploads/2023/12/24/matrix2.png)
27+
**Input:** nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
2428

25-
**Input:** matrix = [[3,-1],[5,2]]
29+
**Output:** 2
2630

27-
**Output:** [[3,2],[5,2]]
31+
**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.
2832

29-
**Explanation:** The diagram above shows the elements that are changed (in blue).
33+
Hence, there are 2 subarrays in nums that match the pattern.
3034

3135
**Constraints:**
3236

33-
* `m == matrix.length`
34-
* `n == matrix[i].length`
35-
* `2 <= m, n <= 50`
36-
* `-1 <= matrix[i][j] <= 100`
37-
* The input is generated such that each column contains at least one non-negative integer.
37+
* `2 <= n == nums.length <= 100`
38+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
39+
* `1 <= m == pattern.length < n`
40+
* `-1 <= pattern[i] <= 1`

0 commit comments

Comments
 (0)