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.
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.
Example 1:
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:
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:
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 <= 1050 <= nums[i] <= 1050 <= k <= 105Sort the array first. Then iterate through the sorted numbers to form subsequences. Whenever the difference between the current element and the starting element of this subsequence exceeds k, start a new subsequence. This greedy method ensures that each subsequence uses the minimum elements possible under the condition.
This solution sorts the array and then initializes a counter for subsequences. It iterates over the sorted array, checking the current element's difference with the start of the subsequence. If the difference exceeds k, a new subsequence starts.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) or O(n) depending on the sorting implementation.
A less optimal sorting and sliding technique. First, sort the array. Then use a sliding window approach to manage subsequences efficiently. For each start of the window, expand it until the condition of maximum difference within k becomes false. This solution is insightful but is closer to the greedy approach.
The Python code above first sorts the array, then uses two indices to create a sliding window to determine the range of the current subsequence until the difference condition is breached, then continues.
JavaScript
Time Complexity: O(n log n) due to sorting plus O(n) scan of the array.
Space Complexity: O(1) as no additional space is used.
| Approach | Complexity |
|---|---|
| Greedy Approach by Sorting | Time Complexity: O(n log n) due to sorting. |
| Sliding Window Approach | Time Complexity: O(n log n) due to sorting plus O(n) scan of the array. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,241 views views
Watch 9 more video solutions →Practice Partition Array Such That Maximum Difference Is K with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor