1090. Largest Values From Labels Medium

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



1/**
2 * [1090] Largest Values From Labels
3 *
4 * There is a set of n items. You are given two integer arrays values and labels where the value and the label of the i^th element are values[i] and labels[i] respectively. You are also given two integers numWanted and useLimit.
5 * Choose a subset s of the n elements such that:
6 * 
7 * 	The size of the subset s is less than or equal to numWanted.
8 * 	There are at most useLimit items with the same label in s.
9 * 
10 * The score of a subset is the sum of the values in the subset.
11 * Return the maximum score of a subset s.
12 *  
13 * Example 1:
14 * 
15 * Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
16 * Output: 9
17 * Explanation: The subset chosen is the first, third, and fifth items.
18 * 
19 * Example 2:
20 * 
21 * Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
22 * Output: 12
23 * Explanation: The subset chosen is the first, second, and third items.
24 * 
25 * Example 3:
26 * 
27 * Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1
28 * Output: 16
29 * Explanation: The subset chosen is the first and fourth items.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	n == values.length == labels.length
35 * 	1 <= n <= 2 * 10^4
36 * 	0 <= values[i], labels[i] <= 2 * 10^4
37 * 	1 <= numWanted, useLimit <= n
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/largest-values-from-labels/
43// discuss: https://leetcode.com/problems/largest-values-from-labels/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn largest_vals_from_labels(values: Vec<i32>, labels: Vec<i32>, num_wanted: i32, use_limit: i32) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_1090() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.