diff --git a/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/README.md b/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/README.md index effb6b20c..88cd33bad 100755 --- a/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/README.md +++ b/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution.go b/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution.go index d115ccf5e..23545b3e4 100644 --- a/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution.go +++ b/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution.go @@ -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) } diff --git a/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution_test.go b/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution_test.go index 14ff50eb4..e650fd0d4 100644 --- a/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution_test.go +++ b/leetcode/3101-3200/3105.Longest-Strictly-Increasing-or-Strictly-Decreasing-Subarray/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }