2554. Maximum Number of Integers to Choose From a Range I Medium

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



1/**
2 * [2554] Maximum Number of Integers to Choose From a Range I
3 *
4 * You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules:
5 * 
6 * 	The chosen integers have to be in the range [1, n].
7 * 	Each integer can be chosen at most once.
8 * 	The chosen integers should not be in the array banned.
9 * 	The sum of the chosen integers should not exceed maxSum.
10 * 
11 * Return the maximum number of integers you can choose following the mentioned rules.
12 *  
13 * <strong class="example">Example 1:
14 * 
15 * Input: banned = [1,6,5], n = 5, maxSum = 6
16 * Output: 2
17 * Explanation: You can choose the integers 2 and 4.
18 * 2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
19 * 
20 * <strong class="example">Example 2:
21 * 
22 * Input: banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
23 * Output: 0
24 * Explanation: You cannot choose any integer while following the mentioned conditions.
25 * 
26 * <strong class="example">Example 3:
27 * 
28 * Input: banned = [11], n = 7, maxSum = 50
29 * Output: 7
30 * Explanation: You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
31 * They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
32 * 
33 *  
34 * Constraints:
35 * 
36 * 	1 <= banned.length <= 10^4
37 * 	1 <= banned[i], n <= 10^4
38 * 	1 <= maxSum <= 10^9
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/
44// discuss: https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn max_count(banned: Vec<i32>, n: i32, max_sum: i32) -> i32 {
50        0
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_2554() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.