Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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]]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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])
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading