3684. Maximize Sum of At Most K Distinct Elements Easy

@problem@discussion
#Array#Hash Table#Greedy#Sorting



1/**
2 * [3684] Maximize Sum of At Most K Distinct Elements
3 *
4 * You are given a positive integer array nums and an integer k.
5 * Choose at most k elements from nums so that their sum is maximized. However, the chosen numbers must be distinct.
6 * Return an array containing the chosen numbers in strictly descending order.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [84,93,100,77,90], k = 3</span>
11 * Output: <span class="example-io">[100,93,90]</span>
12 * Explanation:
13 * The maximum sum is 283, which is attained by choosing 93, 100 and 90. We rearrange them in strictly descending order as [100, 93, 90].
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">nums = [84,93,100,77,93], k = 3</span>
18 * Output: <span class="example-io">[100,93,84]</span>
19 * Explanation:
20 * The maximum sum is 277, which is attained by choosing 84, 93 and 100. We rearrange them in strictly descending order as [100, 93, <span class="example-io">84</span>]. We cannot choose 93, 100 and 93 because the chosen numbers must be distinct.
21 * </div>
22 * <strong class="example">Example 3:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [1,1,1,2,2,2], k = 6</span>
25 * Output: <span class="example-io">[2,1]</span>
26 * Explanation:
27 * The maximum sum is 3, which is attained by choosing 1 and 2. We rearrange them in strictly descending order as [2, 1].
28 * </div>
29 *  
30 * Constraints:
31 * 
32 * 	1 <= nums.length <= 100
33 * 	1 <= nums[i] <= 10^9
34 * 	1 <= k <= nums.length
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/maximize-sum-of-at-most-k-distinct-elements/
40// discuss: https://leetcode.com/problems/maximize-sum-of-at-most-k-distinct-elements/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn max_k_distinct(nums: Vec<i32>, k: i32) -> Vec<i32> {
46        vec![]
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_3684() {
58    }
59}
60

Back
© 2026 bowen.ge All Rights Reserved.