Maximum Number of Integers to Choose From a Range I - Problem
You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules:
- The chosen integers have to be in the range
[1, n]. - Each integer can be chosen at most once.
- The chosen integers should not be in the array
banned. - The sum of the chosen integers should not exceed
maxSum.
Return the maximum number of integers you can choose following the mentioned rules.
Input & Output
Example 1 — Basic Selection
$
Input:
banned = [1,6,5], n = 5, maxSum = 6
›
Output:
2
💡 Note:
Available numbers are [2,3,4]. We can pick 2 and 4 (sum=6≤6) for maximum count of 2.
Example 2 — No Banned Numbers
$
Input:
banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
›
Output:
0
💡 Note:
Only number 8 is available, but 8 > maxSum=1, so we can't pick any numbers.
Example 3 — Small Range
$
Input:
banned = [11], n = 7, maxSum = 50
›
Output:
7
💡 Note:
Numbers [1,2,3,4,5,6,7] are all available. Sum = 1+2+3+4+5+6+7 = 28 ≤ 50, so pick all 7.
Constraints
- 1 ≤ banned.length ≤ 104
- 1 ≤ banned[i], n ≤ 104
- 1 ≤ maxSum ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Range [1,n], banned numbers, and maxSum constraint
2
Greedy Selection
Pick smallest available numbers first
3
Count Result
Return total count of selected numbers
Key Takeaway
🎯 Key Insight: Greedily pick the smallest available numbers to maximize count within the sum budget
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code