Problem

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.

Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k.

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.

Examples

Example 1

1
2
3
4
5
6
7
Input: nums = [3,6,1,2,5], k = 2
Output: 2
Explanation:
We can partition nums into the two subsequences [3,1,2] and [6,5].
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.

Example 2

1
2
3
4
5
6
7
Input: nums = [1,2,3], k = 1
Output: 2
Explanation:
We can partition nums into the two subsequences [1,2] and [3].
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
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].

Example 3

1
2
3
4
5
6
7
8
Input: nums = [2,2,4,5], k = 0
Output: 3
Explanation:
We can partition nums into the three subsequences [2,2], [4], and [5].
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5
  • 0 <= k <= 10^5

Solution

Method 1 - Greedy + Sorting

Intuition

To minimize the number of subsequences, group as many close numbers as possible together. Sorting helps us efficiently form groups where the difference between the smallest and largest is at most k.

Approach

  1. Sort the array nums in non-decreasing order.
  2. Initialize a counter ans to 1 (at least one subsequence is needed).
  3. Track the minimum value start of the current subsequence.
  4. Iterate through the sorted array:
  • If the current number minus start exceeds k, start a new subsequence, increment ans, and update start to the current number.
  1. Return ans.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
   int partitionArray(vector<int>& nums, int k) {
      sort(nums.begin(), nums.end());
      int ans = 1, start = nums[0];
      for (int n : nums) {
        if (n - start > k) {
           ans++;
           start = n;
        }
      }
      return ans;
   }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func partitionArray(nums []int, k int) int {
   sort.Ints(nums)
   ans := 1
   start := nums[0]
   for _, n := range nums {
      if n-start > k {
        ans++
        start = n
      }
   }
   return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
   public int partitionArray(int[] nums, int k) {
      Arrays.sort(nums);
      int ans = 1, start = nums[0];
      for (int n : nums) {
        if (n - start > k) {
           ans++;
           start = n;
        }
      }
      return ans;
   }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
   fun partitionArray(nums: IntArray, k: Int): Int {
      nums.sort()
      var ans = 1
      var start = nums[0]
      for (n in nums) {
        if (n - start > k) {
           ans++
           start = n
        }
      }
      return ans
   }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
   def partitionArray(self, nums: list[int], k: int) -> int:
      nums.sort()
      ans = 1
      start = nums[0]
      for n in nums:
        if n - start > k:
           ans += 1
           start = n
      return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
impl Solution {
   pub fn partition_array(nums: Vec<i32>, k: i32) -> i32 {
      let mut nums = nums;
      nums.sort();
      let mut ans = 1;
      let mut start = nums[0];
      for &n in &nums {
        if n - start > k {
           ans += 1;
           start = n;
        }
      }
      ans
   }
}

Complexity

  • ⏰ Time complexity: O(n log n) (due to sorting)
  • 🧺 Space complexity: O(1) (ignoring sort space, or O(n) if sort is not in-place)