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 mostk.
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.
Input: nums =[3,6,1,2,5], k =2Output: 2Explanation:
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 is3-1=2.The difference between the maximum and minimum value in the second subsequence is6-5=1.Since two subsequences were created, we return2. It can be shown that 2is the minimum number of subsequences needed.
Input: nums =[1,2,3], k =1Output: 2Explanation:
We can partition nums into the two subsequences [1,2] and [3].The difference between the maximum and minimum value in the first subsequence is2-1=1.The difference between the maximum and minimum value in the second subsequence is3-3=0.Since two subsequences were created, we return2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
Input: nums =[2,2,4,5], k =0Output: 3Explanation:
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 is2-2=0.The difference between the maximum and minimum value in the second subsequences is4-4=0.The difference between the maximum and minimum value in the third subsequences is5-5=0.Since three subsequences were created, we return3. It can be shown that 3is the minimum number of subsequences needed.
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.
classSolution {
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
funcpartitionArray(nums []int, kint) int {
sort.Ints(nums)
ans:=1start:=nums[0]
for_, n:=rangenums {
ifn-start > k {
ans++start = n }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintpartitionArray(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
classSolution {
funpartitionArray(nums: IntArray, k: Int): Int {
nums.sort()
var ans = 1var 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
classSolution:
defpartitionArray(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 {
pubfnpartition_array(nums: Vec<i32>, k: i32) -> i32 {
letmut nums = nums;
nums.sort();
letmut ans =1;
letmut start = nums[0];
for&n in&nums {
if n - start > k {
ans +=1;
start = n;
}
}
ans
}
}