3659. Partition Array Into K-Distinct Groups Medium

@problem@discussion
#Array#Hash Table#Counting



1/**
2 * [3659] Partition Array Into K-Distinct Groups
3 *
4 * You are given an integer array nums and an integer k.
5 * Your task is to determine whether it is possible to partition all elements of nums into one or more groups such that:
6 * 
7 * 	Each group contains exactly k elements.
8 * 	All elements in each group are distinct.
9 * 	Each element in nums must be assigned to exactly one group.
10 * 
11 * Return true if such a partition is possible, otherwise return false.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">nums = [1,2,3,4], k = 2</span>
16 * Output: <span class="example-io">true</span>
17 * Explanation:
18 * One possible partition is to have 2 groups:
19 * 
20 * 	Group 1: [1, 2]
21 * 	Group 2: [3, 4]
22 * 
23 * Each group contains k = 2 distinct elements, and all elements are used exactly once.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">nums = [3,5,2,2], k = 2</span>
28 * Output: <span class="example-io">true</span>
29 * Explanation:
30 * One possible partition is to have 2 groups:
31 * 
32 * 	Group 1: [2, 3]
33 * 	Group 2: [2, 5]
34 * 
35 * Each group contains k = 2 distinct elements, and all elements are used exactly once.
36 * </div>
37 * <strong class="example">Example 3:
38 * <div class="example-block">
39 * Input: <span class="example-io">nums = [1,5,2,3], k = 3</span>
40 * Output: <span class="example-io">false</span>
41 * Explanation:
42 * We cannot form groups of k = 3 distinct elements using all values exactly once.
43 * </div>
44 *  
45 * Constraints:
46 * 
47 * 	1 <= nums.length <= 10^5
48 * 	1 <= nums[i] <= 10^5
49 * 	^​​​​​​​1 <= k <= nums.length
50 * 
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/partition-array-into-k-distinct-groups/
55// discuss: https://leetcode.com/problems/partition-array-into-k-distinct-groups/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn partition_array(nums: Vec<i32>, k: i32) -> bool {
61        false
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_3659() {
73    }
74}
75

Back
© 2026 bowen.ge All Rights Reserved.