You have n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and rocks. The ith bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also given an integer additionalRocks, the number of additional rocks you can place in any of the bags.
Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags.
Example 1:
Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2 Output: 3 Explanation: Place 1 rock in bag 0 and 1 rock in bag 1. The number of rocks in each bag are now [2,3,4,4]. Bags 0, 1, and 2 have full capacity. There are 3 bags at full capacity, so we return 3. It can be shown that it is not possible to have more than 3 bags at full capacity. Note that there may be other ways of placing the rocks that result in an answer of 3.
Example 2:
Input: capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100 Output: 3 Explanation: Place 8 rocks in bag 0 and 2 rocks in bag 2. The number of rocks in each bag are now [10,2,2]. Bags 0, 1, and 2 have full capacity. There are 3 bags at full capacity, so we return 3. It can be shown that it is not possible to have more than 3 bags at full capacity. Note that we did not use all of the additional rocks.
Constraints:
n == capacity.length == rocks.length1 <= n <= 5 * 1041 <= capacity[i] <= 1090 <= rocks[i] <= capacity[i]1 <= additionalRocks <= 109When 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 method for this problem is like trying out every single combination of how to fill the bags with the extra rocks we have. We check each combination to see how many bags we can completely fill. Then, we pick the combination that allows us to fill the most bags.
Here's how the algorithm would work step-by-step:
def maximum_bags_brute_force(capacity, rocks, additional_rocks):
max_full_bags = 0
number_of_bags = len(capacity)
# Iterate through all possible distributions of additional rocks
for i in range(2**(number_of_bags)):
current_rocks = additional_rocks
full_bags_count = 0
temp_rocks = rocks[:]
# Represents a distribution of rocks among bags.
for bag_index in range(number_of_bags):
if (i >> bag_index) & 1:
needed_rocks = capacity[bag_index] - temp_rocks[bag_index]
# If we can fill this bag, do so.
if needed_rocks > 0 and current_rocks >= needed_rocks:
current_rocks -= needed_rocks
temp_rocks[bag_index] += needed_rocks
# Count how many bags are full.
for bag_index in range(number_of_bags):
if temp_rocks[bag_index] == capacity[bag_index]:
full_bags_count += 1
# Update the maximum number of full bags.
max_full_bags = max(max_full_bags, full_bags_count)
return max_full_bagsThe best way to maximize the number of bags with full capacity is to prioritize filling the bags that require the least amount of additional rocks first. This greedy approach ensures we use our available rocks most efficiently. We determine the 'need' for each bag, then fill the 'neediest' bags first.
Here's how the algorithm would work step-by-step:
def maximum_bags_with_full_capacity(capacity, rocks, additional_rocks):
number_of_bags = len(capacity)
rocks_needed = []
for i in range(number_of_bags):
rocks_needed.append(capacity[i] - rocks[i])
# Sort bags by how many more rocks they need.
bags_with_index = sorted(range(number_of_bags), key=lambda i: rocks_needed[i])
full_bags_count = 0
# Iterate through bags in ascending order of rocks needed
for bag_index in bags_with_index:
needed_rocks_for_bag = rocks_needed[bag_index]
# Check if we can fill the current bag completely
if additional_rocks >= needed_rocks_for_bag:
additional_rocks -= needed_rocks_for_bag
full_bags_count += 1
# If not enough rocks, then stop. Bags are sorted.
else:
break
return full_bags_count| Case | How to Handle |
|---|---|
| Empty `capacity` or `rocks` array | Return 0 as no bags exist to fill. |
| `capacity` or `rocks` array is null | Throw an IllegalArgumentException or return 0, depending on requirements. |
| Different lengths of `capacity` and `rocks` arrays | Throw an IllegalArgumentException as the inputs are invalid. |
| All bags already have full capacity (rocks[i] == capacity[i] for all i) | Return the length of the capacity array, as all bags are full. |
| All bags have zero capacity (capacity[i] == 0 for all i) | Return the length of the capacity array, as all bags are already full. |
| Total available rocks are insufficient to fill any bag completely | Return 0 as no bags can be filled. |
| Large array sizes causing potential integer overflow when calculating rocks needed | Use long data type for intermediate calculations to prevent overflow and ensure correct sorting/comparison. |
| `rocks[i]` > `capacity[i]` for any i | Throw IllegalArgumentException, treat rocks[i] as capacity[i] or set rocks[i] to capacity[i] (depending on requirements), and continue. |