|
| 1 | +<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You may partition <code>nums</code> into one or more <strong>subsequences</strong> such that each element in <code>nums</code> appears in <strong>exactly</strong> one of the subsequences.</p> |
| 2 | + |
| 3 | +<p>Return <em>the <strong>minimum </strong>number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is <strong>at most</strong> </em><code>k</code><em>.</em></p> |
| 4 | + |
| 5 | +<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> nums = [3,6,1,2,5], k = 2 |
| 12 | +<strong>Output:</strong> 2 |
| 13 | +<strong>Explanation:</strong> |
| 14 | +We can partition nums into the two subsequences [3,1,2] and [6,5]. |
| 15 | +The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2. |
| 16 | +The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1. |
| 17 | +Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed. |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> nums = [1,2,3], k = 1 |
| 24 | +<strong>Output:</strong> 2 |
| 25 | +<strong>Explanation:</strong> |
| 26 | +We can partition nums into the two subsequences [1,2] and [3]. |
| 27 | +The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1. |
| 28 | +The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0. |
| 29 | +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]. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong class="example">Example 3:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> nums = [2,2,4,5], k = 0 |
| 36 | +<strong>Output:</strong> 3 |
| 37 | +<strong>Explanation:</strong> |
| 38 | +We can partition nums into the three subsequences [2,2], [4], and [5]. |
| 39 | +The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0. |
| 40 | +The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0. |
| 41 | +The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0. |
| 42 | +Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed. |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 50 | + <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> |
| 51 | + <li><code>0 <= k <= 10<sup>5</sup></code></li> |
| 52 | +</ul> |
0 commit comments