diff --git a/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/README.md b/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/README.md index 6f63cbb59..86665b299 100755 --- a/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/README.md +++ b/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/README.md @@ -1,28 +1,45 @@ # [3578.Count Partitions With Max-Min Difference at Most K][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 integer array `nums` and an integer `k`. Your task is to partition nums into one or more **non-empty** contiguous segments such that in each segment, the difference between its **maximum** and **minimum** elements is **at most** `k`. + +Return the total number of ways to partition `nums` under this condition. + +Since the answer may be too large, return it **modulo** `10^9 + 7`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [9,4,1,3,7], k = 4 + +Output: 6 + +Explanation: -## 题意 -> ... +There are 6 valid partitions where the difference between the maximum and minimum elements in each segment is at most k = 4: -## 题解 +[[9], [4], [1], [3], [7]] +[[9], [4], [1], [3, 7]] +[[9], [4], [1, 3], [7]] +[[9], [4, 1], [3], [7]] +[[9], [4, 1], [3, 7]] +[[9], [4, 1, 3], [7]] +``` + +**Example 2:** -### 思路1 -> ... -Count Partitions With Max-Min Difference at Most K -```go ``` +Input: nums = [3,3,4], k = 0 + +Output: 2 +Explanation: + +There are 2 valid partitions that satisfy the given conditions: + +[[3], [3], [4]] +[[3, 3], [4]] +``` ## 结语 diff --git a/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution.go b/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution.go index d115ccf5e..2e4003706 100644 --- a/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution.go +++ b/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution.go @@ -1,5 +1,47 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + n := len(nums) + mod := int64(1e9 + 7) + dp := make([]int64, n+1) + prefix := make([]int64, n+1) + minQ := make([]int, 0) + maxQ := make([]int, 0) + + dp[0] = 1 + prefix[0] = 1 + + for i, j := 0, 0; i < n; i++ { + // maintain the maximum value queue + for len(maxQ) > 0 && nums[maxQ[len(maxQ)-1]] <= nums[i] { + maxQ = maxQ[:len(maxQ)-1] + } + maxQ = append(maxQ, i) + // maintain the minimum value queue + for len(minQ) > 0 && nums[minQ[len(minQ)-1]] >= nums[i] { + minQ = minQ[:len(minQ)-1] + } + minQ = append(minQ, i) + + // adjust window + for len(maxQ) > 0 && len(minQ) > 0 && + nums[maxQ[0]]-nums[minQ[0]] > k { + if maxQ[0] == j { + maxQ = maxQ[1:] + } + if minQ[0] == j { + minQ = minQ[1:] + } + j++ + } + + if j > 0 { + dp[i+1] = (prefix[i] - prefix[j-1] + mod) % mod + } else { + dp[i+1] = prefix[i] % mod + } + prefix[i+1] = (prefix[i] + dp[i+1]) % mod + } + + return int(dp[n]) } diff --git a/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution_test.go b/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution_test.go index 14ff50eb4..44c09064b 100644 --- a/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution_test.go +++ b/leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{9, 4, 1, 3, 7}, 4, 6}, + {"TestCase2", []int{3, 3, 4}, 0, 2}, } // 开始测试 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() { }