Skip to content

Commit 47de1d2

Browse files
authored
Merge pull request #1191 from 0xff-dev/2294
Add solution and test-cases for problem 2294
2 parents 7f9e1ec + 521bbea commit 47de1d2

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [2294.Partition Array Such That Maximum Difference Is 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`. You may partition `nums` into one or more **subsequences** such that each element in `nums` appears in **exactly** one of the subsequences.
5+
6+
Return the **minimum** number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is **at most** `k`.
7+
8+
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [3,6,1,2,5], k = 2
14+
Output: 2
15+
Explanation:
16+
We can partition nums into the two subsequences [3,1,2] and [6,5].
17+
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
18+
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
19+
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
1320
```
1421

15-
## 题意
16-
> ...
17-
18-
## 题解
22+
**Example 2:**
1923

20-
### 思路1
21-
> ...
22-
Partition Array Such That Maximum Difference Is K
23-
```go
2424
```
25+
Input: nums = [1,2,3], k = 1
26+
Output: 2
27+
Explanation:
28+
We can partition nums into the two subsequences [1,2] and [3].
29+
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
30+
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
31+
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
32+
```
33+
34+
**Example 3:**
2535

36+
```
37+
Input: nums = [2,2,4,5], k = 0
38+
Output: 3
39+
Explanation:
40+
We can partition nums into the three subsequences [2,2], [4], and [5].
41+
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
42+
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
43+
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
44+
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
45+
```
2646

2747
## 结语
2848

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int) int {
6+
sort.Ints(nums)
7+
ans := 0
8+
start := 0
9+
l := len(nums)
10+
for start < l {
11+
i := sort.Search(l, func(i int) bool {
12+
return nums[i] > nums[start]+k
13+
})
14+
ans++
15+
start = i
16+
}
17+
return ans
518
}

leetcode/2201-2300/2294.Partition-Array-Such-That-Maximum-Difference-Is-K/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ 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{3, 6, 1, 2, 5}, 2, 2},
18+
{"TestCase2", []int{1, 2, 3}, 1, 2},
19+
{"TestCase3", []int{2, 2, 4, 5}, 0, 3},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)