Maximum Bags With Full Capacity of Rocks

We can approach this problem by iterating through the bags and attempting to fill them using the available additionalRocks. We’ll sort the bags by the number of rocks needed to reach full capacity, so we can prioritize filling the bags that require fewer rocks.

Here’s a step-by-step solution:

  1. Calculate the number of rocks needed for each bag to reach full capacity and store this information in a list.
  2. Sort the list in ascending order.
  3. Iterate through the sorted list, filling bags until you run out of additionalRocks.
  4. Return the number of bags that have reached full capacity.

Below is the code that implements these steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int:
        # Step 1: Calculate the number of rocks needed for each bag to reach full capacity
        rocks_needed = [capacity[i] - rocks[i] for i in range(len(capacity))]

        # Step 2: Sort the list in ascending order
        rocks_needed.sort()

        # Step 3: Iterate through the sorted list and fill the bags
        full_bags = 0
        for rocks_required in rocks_needed:
            if additionalRocks >= rocks_required:
                full_bags += 1
                additionalRocks -= rocks_required
            else:
                break

        # Step 4: Return the number of bags that have reached full capacity
        return full_bags

This code will return the maximum number of bags that could have full capacity after placing the additionalRocks in some bags.