2600. K Items With the Maximum Sum Easy

@problem@discussion
#Math#Greedy



1/**
2 * [2600] K Items With the Maximum Sum
3 *
4 * There is a bag that consists of items, each item has a number 1, 0, or -1 written on it.
5 * You are given four non-negative integers numOnes, numZeros, numNegOnes, and k.
6 * The bag initially contains:
7 * 
8 * 	numOnes items with 1s written on them.
9 * 	numZeroes items with 0s written on them.
10 * 	numNegOnes items with -1s written on them.
11 * 
12 * We want to pick exactly k items among the available items. Return the maximum possible sum of numbers written on the items.
13 *  
14 * <strong class="example">Example 1:
15 * 
16 * Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
17 * Output: 2
18 * Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
19 * It can be proven that 2 is the maximum possible sum.
20 * 
21 * <strong class="example">Example 2:
22 * 
23 * Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
24 * Output: 3
25 * Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
26 * It can be proven that 3 is the maximum possible sum.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	0 <= numOnes, numZeros, numNegOnes <= 50
32 * 	0 <= k <= numOnes + numZeros + numNegOnes
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/k-items-with-the-maximum-sum/
38// discuss: https://leetcode.com/problems/k-items-with-the-maximum-sum/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_2600() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.