Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
# [3105.Longest Strictly Increasing or Strictly Decreasing Subarray][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,4,3,3,2]

Output: 2

Explanation:

The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].

The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].

Hence, we return 2.
```

**Example 2:**

```
Input: nums = [3,3,3,3]

Output: 1

## 题意
> ...
Explanation:

## 题解
The strictly increasing subarrays of nums are [3], [3], [3], and [3].

### 思路1
> ...
Longest Strictly Increasing or Strictly Decreasing Subarray
```go
The strictly decreasing subarrays of nums are [3], [3], [3], and [3].

Hence, we return 1.
```

**Example 3:**

```
Input: nums = [3,2,1]

Output: 3

Explanation:

The strictly increasing subarrays of nums are [3], [2], and [1].

The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].

Hence, we return 3.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func helper(nums []int, less func(i, j int) bool) int {
l := 1
res := 1
for i := 1; i < len(nums); i++ {
if less(nums[i-1], nums[i]) {
l++
res = max(res, l)
continue
}
l = 1
}
return max(res, l)
}

func Solution(nums []int) int {
a := helper(nums, func(i, j int) bool {
return i < j
})
b := helper(nums, func(i, j int) bool {
return i > j
})
return max(a, b)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 4, 3, 3, 2}, 2},
{"TestCase2", []int{3, 3, 3, 3}, 1},
{"TestCase3", []int{3, 2, 1}, 3},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading