Skip to content

Commit 80550e3

Browse files
authored
Merge pull request #1343 from 0xff-dev/3350
Add solution and test-cases for problem 3350
2 parents bcac188 + 326ef27 commit 80550e3

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [3350.Adjacent Increasing Subarrays Detection II][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, your task is to find the **maximum** value of `k` for which there exist **two** adjacent subarrays of length `k` each, such that both subarrays are **strictly increasing**. Specifically, check if there are **two** subarrays of length `k` 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**.
7+
- The subarrays must be **adjacent**, meaning `b = a + k`.
8+
9+
Return the **maximum** possible value of `k`.
10+
11+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
Input: nums = [2,5,7,8,9,2,3,4,3,1]
1417
15-
## 题意
16-
> ...
18+
Output: 3
1719
18-
## 题解
20+
Explanation:
1921
20-
### 思路1
21-
> ...
22-
Adjacent Increasing Subarrays Detection II
23-
```go
22+
The subarray starting at index 2 is [7, 8, 9], which is strictly increasing.
23+
The subarray starting at index 5 is [2, 3, 4], which is also strictly increasing.
24+
These two subarrays are adjacent, and 3 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.
2425
```
2526

27+
**Example 2:**
28+
29+
```
30+
Input: nums = [1,2,3,4,4,4,4,5,6,7]
31+
32+
Output: 2
33+
34+
Explanation:
35+
36+
The subarray starting at index 0 is [1, 2], which is strictly increasing.
37+
The subarray starting at index 2 is [3, 4], which is also strictly increasing.
38+
These two subarrays are adjacent, and 2 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist.
39+
```
2640

2741
## 结语
2842

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func ok(nums []int, k int) bool {
6+
// 存储所有ok的字数组的下标
7+
// 然后找index的间隔是否存在不想等就可以了
8+
indies := []int{}
9+
start, end := 0, 0
10+
curLen := 0
11+
pre := -1001
12+
// 6, 13, -17, -20, 2
13+
for ; end < len(nums); end++ {
14+
if nums[end] <= pre {
15+
start, curLen = end, 1
16+
} else {
17+
curLen++
18+
}
19+
pre = nums[end]
20+
if curLen == k {
21+
indies = append(indies, start)
22+
start++
23+
curLen--
24+
}
25+
}
26+
keys := make(map[int]struct{})
27+
for _, index := range indies {
28+
keys[index] = struct{}{}
29+
}
30+
for i := 0; i < len(indies)-1; i++ {
31+
if _, ok := keys[indies[i]+k]; ok {
32+
return true
33+
}
34+
}
35+
return false
36+
}
37+
func Solution(nums []int) int {
38+
index := sort.Search(len(nums)/2, func(i int) bool {
39+
return !ok(nums, i+1)
40+
})
41+
return index
542
}

leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution_test.go

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)