Skip to content

Commit cc62e7f

Browse files
authored
Merge pull request #1342 from 0xff-dev/3349
Add solution and test-cases for problem 3349
2 parents 56f1d96 + 9938684 commit cc62e7f

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [3349.Adjacent Increasing Subarrays Detection 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+
Given an array `nums` of `n` integers and an integer `k`, determine whether there exist **two adjacent** subarrays of length `k` such that both subarrays are **strictly increasing**. Specifically, check if there are two subarrays starting at indices `a` and `b` (`a < b`), where:
5+
6+
- Both subarrays `nums[a..a + k - 1]` and `nums[b..b + k - 1]` are **strictly increasing*8.
7+
- The subarrays must be **adjacent**, meaning `b = a + k`.
8+
9+
Return `true` if it is possible to find **two** such subarrays, and `false` otherwise.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
14+
Input: nums = [2,5,7,8,9,2,3,4,3,1], k = 3
1415
15-
## 题意
16-
> ...
16+
Output: true
1717
18-
## 题解
18+
Explanation:
1919
20-
### 思路1
21-
> ...
22-
Adjacent Increasing Subarrays Detection I
23-
```go
20+
The subarray starting at index 2 is [7, 8, 9], which is strictly increasing.
21+
The subarray starting at index 5 is [2, 3, 4], which is also strictly increasing.
22+
These two subarrays are adjacent, so the result is true.
2423
```
2524

25+
**Example 2:**
26+
27+
```
28+
Input: nums = [1,2,3,4,4,4,4,5,6,7], k = 5
29+
30+
Output: false
31+
```
2632

2733
## 结语
2834

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, k int) bool {
4+
indies := map[int]struct{}{}
5+
start, end := 0, 0
6+
curLen := 0
7+
pre := -1001
8+
for ; end < len(nums); end++ {
9+
if nums[end] <= pre {
10+
start, curLen = end, 1
11+
} else {
12+
curLen++
13+
}
14+
pre = nums[end]
15+
if curLen == k {
16+
indies[start] = struct{}{}
17+
start++
18+
curLen--
19+
}
20+
}
21+
for index := range indies {
22+
if _, ok := indies[index+k]; ok {
23+
return true
24+
}
25+
}
26+
return false
527
}

leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
14+
k int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 5, 7, 8, 9, 2, 3, 4, 3, 1}, 3, true},
18+
{"TestCase2", []int{1, 2, 3, 4, 4, 4, 4, 5, 6, 7}, 5, false},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)