Skip to content

Commit c7a29ac

Browse files
authored
Merge pull request #1264 from 0xff-dev/3034
Add solution and test-cases for problem 3034
2 parents 5cf842f + 7b3cf84 commit c7a29ac

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [3034.Number of Subarrays That Match a Pattern I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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`.
5+
6+
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]`:
7+
8+
- `nums[i + k + 1] > nums[i + k] if pattern[k] == 1`.
9+
- `nums[i + k + 1] == nums[i + k] if pattern[k] == 0`.
10+
- `nums[i + k + 1] < nums[i + k] if pattern[k] == -1`.
11+
12+
Return the **count** of subarrays in nums that match the `pattern`.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,2,3,4,5,6], pattern = [1,1]
18+
Output: 4
19+
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.
20+
Hence, there are 4 subarrays in nums that match the pattern.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of Subarrays That Match a Pattern I
23-
```go
2425
```
25-
26+
Input: nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
27+
Output: 2
28+
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.
29+
Hence, there are 2 subarrays in nums that match the pattern.
30+
```
2631

2732
## 结语
2833

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, pattern []int) int {
4+
var ans int
5+
m := len(pattern)
6+
n := len(nums)
7+
k := 0
8+
for i := 0; i < n-m; i++ {
9+
k = 0
10+
for ; k < m; k++ {
11+
if pattern[k] == 1 && nums[i+k+1] > nums[i+k] {
12+
continue
13+
}
14+
if pattern[k] == 0 && nums[i+k+1] == nums[i+k] {
15+
continue
16+
}
17+
if pattern[k] == -1 && nums[i+k+1] < nums[i+k] {
18+
continue
19+
}
20+
break
21+
}
22+
if k == m {
23+
ans++
24+
}
25+
}
26+
return ans
527
}

leetcode/3001-3100/3034.Number-of-Subarrays-That-Match-a-Pattern-I/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs, pattern []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3, 4, 5, 6}, []int{1, 1}, 4},
17+
{"TestCase2", []int{1, 4, 4, 1, 3, 5, 5, 3}, []int{1, 0, -1}, 2},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.inputs, c.pattern)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.inputs, c.pattern)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)