Skip to content

Commit 7bdd75a

Browse files
authored
Merge pull request #1388 from 0xff-dev/3578
Add solution and test-cases for proble 3578
2 parents 8761507 + 7123b3c commit 7bdd75a

File tree

3 files changed

+84
-25
lines changed

3 files changed

+84
-25
lines changed

leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3578.Count Partitions With Max-Min Difference at Most K][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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`.
5+
6+
Return the total number of ways to partition `nums` under this condition.
7+
8+
Since the answer may be too large, return it **modulo** `10^9 + 7`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
13+
Input: nums = [9,4,1,3,7], k = 4
14+
15+
Output: 6
16+
17+
Explanation:
1418
15-
## 题意
16-
> ...
19+
There are 6 valid partitions where the difference between the maximum and minimum elements in each segment is at most k = 4:
1720
18-
## 题解
21+
[[9], [4], [1], [3], [7]]
22+
[[9], [4], [1], [3, 7]]
23+
[[9], [4], [1, 3], [7]]
24+
[[9], [4, 1], [3], [7]]
25+
[[9], [4, 1], [3, 7]]
26+
[[9], [4, 1, 3], [7]]
27+
```
28+
29+
**Example 2:**
1930

20-
### 思路1
21-
> ...
22-
Count Partitions With Max-Min Difference at Most K
23-
```go
2431
```
32+
Input: nums = [3,3,4], k = 0
33+
34+
Output: 2
2535
36+
Explanation:
37+
38+
There are 2 valid partitions that satisfy the given conditions:
39+
40+
[[3], [3], [4]]
41+
[[3, 3], [4]]
42+
```
2643

2744
## 结语
2845

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
n := len(nums)
5+
mod := int64(1e9 + 7)
6+
dp := make([]int64, n+1)
7+
prefix := make([]int64, n+1)
8+
minQ := make([]int, 0)
9+
maxQ := make([]int, 0)
10+
11+
dp[0] = 1
12+
prefix[0] = 1
13+
14+
for i, j := 0, 0; i < n; i++ {
15+
// maintain the maximum value queue
16+
for len(maxQ) > 0 && nums[maxQ[len(maxQ)-1]] <= nums[i] {
17+
maxQ = maxQ[:len(maxQ)-1]
18+
}
19+
maxQ = append(maxQ, i)
20+
// maintain the minimum value queue
21+
for len(minQ) > 0 && nums[minQ[len(minQ)-1]] >= nums[i] {
22+
minQ = minQ[:len(minQ)-1]
23+
}
24+
minQ = append(minQ, i)
25+
26+
// adjust window
27+
for len(maxQ) > 0 && len(minQ) > 0 &&
28+
nums[maxQ[0]]-nums[minQ[0]] > k {
29+
if maxQ[0] == j {
30+
maxQ = maxQ[1:]
31+
}
32+
if minQ[0] == j {
33+
minQ = minQ[1:]
34+
}
35+
j++
36+
}
37+
38+
if j > 0 {
39+
dp[i+1] = (prefix[i] - prefix[j-1] + mod) % mod
40+
} else {
41+
dp[i+1] = prefix[i] % mod
42+
}
43+
prefix[i+1] = (prefix[i] + dp[i+1]) % mod
44+
}
45+
46+
return int(dp[n])
547
}

leetcode/3501-3600/3578.Count-Partitions-With-Max-Min-Difference-at-Most-K/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{9, 4, 1, 3, 7}, 4, 6},
18+
{"TestCase2", []int{3, 3, 4}, 0, 2},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)