2294. Partition Array Such That Maximum Difference Is K Medium

@problem@discussion
#Array#Greedy#Sorting



1/**
2 * [2294] Partition Array Such That Maximum Difference Is K
3 *
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 * Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k.
6 * 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 *  
8 * Example 1:
9 *
10 * Input: nums = [3,6,1,2,5], k = 2
11 * Output: 2
12 * Explanation:
13 * We can partition nums into the two subsequences [3,1,2] and [6,5].
14 * The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
15 * The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
16 * Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
17 *
18 * Example 2:
19 *
20 * Input: nums = [1,2,3], k = 1
21 * Output: 2
22 * Explanation:
23 * We can partition nums into the two subsequences [1,2] and [3].
24 * The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
25 * The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
26 * 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].
27 *
28 * Example 3:
29 *
30 * Input: nums = [2,2,4,5], k = 0
31 * Output: 3
32 * Explanation:
33 * We can partition nums into the three subsequences [2,2], [4], and [5].
34 * The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
35 * The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
36 * The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
37 * Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
38 *
39 *  
40 * Constraints:
41 *
42 * 1 <= nums.length <= 10^5
43 * 0 <= nums[i] <= 10^5
44 * 0 <= k <= 10^5
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/
50// discuss: https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55    pub fn partition_array(nums: Vec<i32>, k: i32) -> i32 {
56        let mut data = nums.clone();
57        data.sort();
58        let mut result = 1;
59        let mut start = 0;
60        for end in 1..data.len() {
61            if data[end] - data[start] > k {
62                result += 1;
63                start = end;
64            }
65        }
66        result
67    }
68}
69
70// submission codes end
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_2294() {
78        assert_eq!(Solution::partition_array(vec![2, 2, 4, 5], 0), 3);
79        assert_eq!(Solution::partition_array(vec![3, 6, 1, 2, 5], 2), 2);
80        assert_eq!(Solution::partition_array(vec![1, 2, 3], 1), 2);
81    }
82}
83


Back
© 2025 bowen.ge All Rights Reserved.