diff --git a/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/README.md b/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/README.md index 8c92bb31e..5406ec968 100755 --- a/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/README.md +++ b/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/README.md @@ -1,28 +1,42 @@ # [3350.Adjacent Increasing Subarrays Detection II][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 +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: + +- Both subarrays `nums[a..a + k - 1]` and `nums[b..b + k - 1]` are **strictly increasing**. +- The subarrays must be **adjacent**, meaning `b = a + k`. + +Return the **maximum** possible value of `k`. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [2,5,7,8,9,2,3,4,3,1] -## 题意 -> ... +Output: 3 -## 题解 +Explanation: -### 思路1 -> ... -Adjacent Increasing Subarrays Detection II -```go +The subarray starting at index 2 is [7, 8, 9], which is strictly increasing. +The subarray starting at index 5 is [2, 3, 4], which is also strictly increasing. +These two subarrays are adjacent, and 3 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist. ``` +**Example 2:** + +``` +Input: nums = [1,2,3,4,4,4,4,5,6,7] + +Output: 2 + +Explanation: + +The subarray starting at index 0 is [1, 2], which is strictly increasing. +The subarray starting at index 2 is [3, 4], which is also strictly increasing. +These two subarrays are adjacent, and 2 is the maximum possible value of k for which two such adjacent strictly increasing subarrays exist. +``` ## 结语 diff --git a/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution.go b/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution.go index d115ccf5e..cd76f87b2 100644 --- a/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution.go +++ b/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution.go @@ -1,5 +1,42 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func ok(nums []int, k int) bool { + // 存储所有ok的字数组的下标 + // 然后找index的间隔是否存在不想等就可以了 + indies := []int{} + start, end := 0, 0 + curLen := 0 + pre := -1001 + // 6, 13, -17, -20, 2 + for ; end < len(nums); end++ { + if nums[end] <= pre { + start, curLen = end, 1 + } else { + curLen++ + } + pre = nums[end] + if curLen == k { + indies = append(indies, start) + start++ + curLen-- + } + } + keys := make(map[int]struct{}) + for _, index := range indies { + keys[index] = struct{}{} + } + for i := 0; i < len(indies)-1; i++ { + if _, ok := keys[indies[i]+k]; ok { + return true + } + } + return false +} +func Solution(nums []int) int { + index := sort.Search(len(nums)/2, func(i int) bool { + return !ok(nums, i+1) + }) + return index } diff --git a/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution_test.go b/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution_test.go index 14ff50eb4..650aeb963 100644 --- a/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution_test.go +++ b/leetcode/3301-3400/3350.Adjacent-Increasing-Subarrays-Detection-II/Solution_test.go @@ -10,12 +10,11 @@ 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{2, 5, 7, 8, 9, 2, 3, 4, 3, 1}, 3}, + {"TestCase2", []int{1, 2, 3, 4, 4, 4, 4, 5, 6, 7}, 2}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }