You're managing a rock collection warehouse with n storage bags, each with different capacities. Your goal is to maximize the number of completely full bags by strategically distributing additional rocks.
Problem Setup:
- You have
nbags numbered from0ton-1 - Each bag
ican hold a maximum ofcapacity[i]rocks - Each bag
icurrently containsrocks[i]rocks - You have
additionalRocksextra rocks to distribute
Goal: Return the maximum number of bags that can reach their full capacity after optimally placing the additional rocks.
Example: If you have bags with capacities [2,3,4,5] and current rocks [1,2,4,4], with 2 additional rocks, you can fill the first bag (needs 1 rock) and second bag (needs 1 rock) to get 2 completely full bags.
Input & Output
Visualization
Time & Space Complexity
Where k is the average rocks needed per bag and n is number of bags. We try all possible ways to distribute rocks.
Recursion stack depth can go up to n levels in the worst case
Constraints
- 1 โค n โค 105
- 0 โค capacity[i], rocks[i] โค 109
- rocks[i] โค capacity[i] for all i
- 0 โค additionalRocks โค 109