|
1 | 1 | # [2294.Partition Array Such That Maximum Difference Is K][title]
|
2 | 2 |
|
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 |
| -
|
6 | 3 | ## 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. |
7 | 9 |
|
8 | 10 | **Example 1:**
|
9 | 11 |
|
10 | 12 | ```
|
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. |
13 | 20 | ```
|
14 | 21 |
|
15 |
| -## 题意 |
16 |
| -> ... |
17 |
| -
|
18 |
| -## 题解 |
| 22 | +**Example 2:** |
19 | 23 |
|
20 |
| -### 思路1 |
21 |
| -> ... |
22 |
| -Partition Array Such That Maximum Difference Is K |
23 |
| -```go |
24 | 24 | ```
|
| 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:** |
25 | 35 |
|
| 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 | +``` |
26 | 46 |
|
27 | 47 | ## 结语
|
28 | 48 |
|
|
0 commit comments