Skip to content

Commit 2e193ea

Browse files
authored
Merge pull request #1111 from 0xff-dev/3105
Add solution and test-cases for problem 3105
2 parents d892e88 + 8b96148 commit 2e193ea

File tree

3 files changed

+68
-21
lines changed

3 files changed

+68
-21
lines changed

leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [3105.Longest Strictly Increasing or Strictly Decreasing Subarray][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 an array of integers `nums`. Return the length of the **longest** subarray of `nums` which is either strictly increasing or strictly decreasing
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums = [1,4,3,3,2]
10+
11+
Output: 2
12+
13+
Explanation:
14+
15+
The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].
16+
17+
The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].
18+
19+
Hence, we return 2.
20+
```
21+
22+
**Example 2:**
23+
1324
```
25+
Input: nums = [3,3,3,3]
26+
27+
Output: 1
1428
15-
## 题意
16-
> ...
29+
Explanation:
1730
18-
## 题解
31+
The strictly increasing subarrays of nums are [3], [3], [3], and [3].
1932
20-
### 思路1
21-
> ...
22-
Longest Strictly Increasing or Strictly Decreasing Subarray
23-
```go
33+
The strictly decreasing subarrays of nums are [3], [3], [3], and [3].
34+
35+
Hence, we return 1.
2436
```
2537

38+
**Example 3:**
39+
40+
```
41+
Input: nums = [3,2,1]
42+
43+
Output: 3
44+
45+
Explanation:
46+
47+
The strictly increasing subarrays of nums are [3], [2], and [1].
48+
49+
The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].
50+
51+
Hence, we return 3.
52+
```
2653

2754
## 结语
2855

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

3-
func Solution(x bool) bool {
4-
return x
3+
func helper(nums []int, less func(i, j int) bool) int {
4+
l := 1
5+
res := 1
6+
for i := 1; i < len(nums); i++ {
7+
if less(nums[i-1], nums[i]) {
8+
l++
9+
res = max(res, l)
10+
continue
11+
}
12+
l = 1
13+
}
14+
return max(res, l)
15+
}
16+
17+
func Solution(nums []int) int {
18+
a := helper(nums, func(i, j int) bool {
19+
return i < j
20+
})
21+
b := helper(nums, func(i, j int) bool {
22+
return i > j
23+
})
24+
return max(a, b)
525
}

leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{1, 4, 3, 3, 2}, 2},
17+
{"TestCase2", []int{3, 3, 3, 3}, 1},
18+
{"TestCase3", []int{3, 2, 1}, 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)