Leetcode Problem 2279. Maximum Bags With Full Capacity of Rocks

2279. Maximum Bags With Full Capacity of Rocks

Leetcode Solutions

Greedy Approach to Maximize Full Capacity Bags

  1. Initialize an array remaining_capacity to store the remaining capacity of each bag.
  2. Calculate the remaining capacity for each bag as capacity[i] - rocks[i] and store it in remaining_capacity.
  3. Sort the remaining_capacity array in ascending order.
  4. Initialize full_bags to 0 to keep track of the number of bags filled to full capacity.
  5. Iterate over the sorted remaining_capacity array: a. For each cap in remaining_capacity, check if additionalRocks is greater than or equal to cap. b. If yes, increment full_bags, decrement additionalRocks by cap, and continue to the next bag. c. If no, break the loop as we cannot fill any more bags.
  6. Return the value of full_bags.
UML Thumbnail

Alternative Approach Using Priority Queue

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...