diff --git a/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/README.md b/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/README.md index 28390af4c..b78d3dd48 100755 --- a/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/README.md +++ b/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/README.md @@ -1,28 +1,34 @@ # [3349.Adjacent Increasing Subarrays Detection I][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 and an integer `k`, determine whether there exist **two adjacent** subarrays of length `k` such that both subarrays are **strictly increasing**. Specifically, check if there are two subarrays 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*8. +- The subarrays must be **adjacent**, meaning `b = a + k`. + +Return `true` if it is possible to find **two** such subarrays, and `false` otherwise. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [2,5,7,8,9,2,3,4,3,1], k = 3 -## 题意 -> ... +Output: true -## 题解 +Explanation: -### 思路1 -> ... -Adjacent Increasing Subarrays Detection I -```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, so the result is true. ``` +**Example 2:** + +``` +Input: nums = [1,2,3,4,4,4,4,5,6,7], k = 5 + +Output: false +``` ## 结语 diff --git a/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution.go b/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution.go index d115ccf5e..a3420421d 100644 --- a/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution.go +++ b/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution.go @@ -1,5 +1,27 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) bool { + indies := map[int]struct{}{} + start, end := 0, 0 + curLen := 0 + pre := -1001 + for ; end < len(nums); end++ { + if nums[end] <= pre { + start, curLen = end, 1 + } else { + curLen++ + } + pre = nums[end] + if curLen == k { + indies[start] = struct{}{} + start++ + curLen-- + } + } + for index := range indies { + if _, ok := indies[index+k]; ok { + return true + } + } + return false } diff --git a/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution_test.go b/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution_test.go index 14ff50eb4..e6133834d 100644 --- a/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution_test.go +++ b/leetcode/3301-3400/3349.Adjacent-Increasing-Subarrays-Detection-I/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs []int + k int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 5, 7, 8, 9, 2, 3, 4, 3, 1}, 3, true}, + {"TestCase2", []int{1, 2, 3, 4, 4, 4, 4, 5, 6, 7}, 5, false}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }