You are given an integer array banned
and two integers n
and maxSum
. You are choosing some number of integers following the below rules:
[1, n]
.banned
.maxSum
.Return the maximum number of integers you can choose following the mentioned rules.
Example 1:
Input: banned = [1,6,5], n = 5, maxSum = 6 Output: 2 Explanation: You can choose the integers 2 and 4. 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.
Example 2:
Input: banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1 Output: 0 Explanation: You cannot choose any integer while following the mentioned conditions.
Example 3:
Input: banned = [11], n = 7, maxSum = 50 Output: 7 Explanation: You can choose the integers 1, 2, 3, 4, 5, 6, and 7. They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
Constraints:
1 <= banned.length <= 104
1 <= banned[i], n <= 104
1 <= maxSum <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves checking every possible combination of numbers within the range. We try including each number one by one, and then see if we can include more numbers while staying within the cost limit and avoiding the banned numbers. We repeat this for every possible starting point.
Here's how the algorithm would work step-by-step:
def max_number_of_integers(number_range, banned_numbers, maximum_cost):
max_integers_selected = 0
for start_number in range(1, number_range + 1):
integers_selected = 0
current_cost = maximum_cost
selected_numbers = []
for current_number in range(start_number, number_range + 1):
if current_number in banned_numbers:
continue
# Check if adding the number exceeds the remaining cost
if current_number > current_cost:
continue
# Deduct the cost and increment selected integers
current_cost -= current_number
integers_selected += 1
selected_numbers.append(current_number)
# Update maximum integers if current selection is better
max_integers_selected = max(max_integers_selected, integers_selected)
return max_integers_selected
The goal is to pick as many numbers as possible from a given range while avoiding certain forbidden numbers, and staying within a budget. The best strategy is to greedily pick the smallest available numbers that aren't forbidden, until you run out of budget.
Here's how the algorithm would work step-by-step:
def max_number_of_integers(number_of_integers, maximum_sum, forbidden_numbers):
available_numbers = []
for number in range(1, number_of_integers + 1):
if number not in forbidden_numbers:
available_numbers.append(number)
available_numbers.sort()
count_of_chosen_integers = 0
current_sum = 0
for number in available_numbers:
# Check if adding the current number exceeds the maximum sum
if current_sum + number <= maximum_sum:
current_sum += number
count_of_chosen_integers += 1
# If the budget is exhausted, stop selecting numbers
else:
break
return count_of_chosen_integers
Case | How to Handle |
---|---|
n is 0 or less | Return 0, as there are no numbers to choose from the range. |
maxSum is 0 or less | Return 0, as no positive integers can be selected without exceeding the sum. |
banned is null or empty | Treat it as if no numbers are banned, proceeding with the selection process. |
banned contains duplicates | The set conversion implicitly handles duplicates, only storing unique banned numbers. |
banned contains numbers outside the range [1, n] | These numbers are irrelevant and should be ignored during selection, as they cannot be picked. |
n is very large, close to Integer.MAX_VALUE | Ensure the running sum does not overflow during integer addition by using long if necessary. |
maxSum is small but n is extremely large | The solution might iterate through most of the numbers from 1 to maxSum before finding a banned number or exceeding maxSum; the algorithm should handle this inefficiency if possible (e.g., optimize the loop condition). |
All numbers in the range [1, n] are banned | Return 0, as no numbers can be selected. |