Maximum Bags With Full Capacity of Rocks - Problem

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 n bags numbered from 0 to n-1
  • Each bag i can hold a maximum of capacity[i] rocks
  • Each bag i currently contains rocks[i] rocks
  • You have additionalRocks extra 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

example_1.py โ€” Basic Case
$ Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2
โ€บ Output: 3
๐Ÿ’ก Note: Bag needs: [1,1,0,1]. We can fill bags needing 0, 1, and 1 rocks respectively, using all 2 additional rocks to get 3 full bags total.
example_2.py โ€” Limited Resources
$ Input: capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100
โ€บ Output: 3
๐Ÿ’ก Note: Bag needs: [8,0,2]. Even with 100 additional rocks, we can only fill all 3 bags since that's the maximum possible.
example_3.py โ€” No Additional Rocks
$ Input: capacity = [2,5,3,4], rocks = [1,3,1,4], additionalRocks = 0
โ€บ Output: 1
๐Ÿ’ก Note: Without additional rocks, only bag 3 (index 3) is already full with rocks[3] = capacity[3] = 4.

Visualization

Tap to expand
Smart Rock Distribution StrategyStep 1: Calculate NeedsCap: 2Need: 1Cap: 3Need: 1Step 2: Sort by NeedFullNeed: 0Need: 1Step 3: Fill Greedily2Rocks Leftโœ“ FilledResult3 bags filled completelyUsing optimal greedy strategy๐Ÿ’ก Key InsightThe greedy approach works because filling cheaper bags firstalways leaves more resources for additional bags.There's never a benefit to filling an expensive bag before a cheaper one.
Understanding the Visualization
1
Calculate Shopping List
Figure out the 'price' (additional rocks needed) for each bag to become full
2
Sort by Price
Arrange bags from cheapest to most expensive in terms of rocks needed
3
Shop Greedily
Buy (fill) bags starting from cheapest until you run out of budget (additional rocks)
4
Count Purchases
The number of bags you successfully filled is your answer
Key Takeaway
๐ŸŽฏ Key Insight: Greedy algorithms work when local optimal choices lead to global optimal solutions. Here, always choosing the 'cheapest' bag to fill maximizes our total count.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k^n)

Where k is the average rocks needed per bag and n is number of bags. We try all possible ways to distribute rocks.

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Recursion stack depth can go up to n levels in the worst case

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค capacity[i], rocks[i] โ‰ค 109
  • rocks[i] โ‰ค capacity[i] for all i
  • 0 โ‰ค additionalRocks โ‰ค 109
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
32.0K Views
Medium-High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen