Skip to content

Latest commit

 

History

History
33 lines (28 loc) · 863 Bytes

Question_2279.md

File metadata and controls

33 lines (28 loc) · 863 Bytes

LeetCode Records - Question 2279 Maximum Bags With Full Capacity of Rocks

Attempt 1: Calculate the remainder for each bag

class Solution {
    public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
        int n = capacity.length;
        for (int i = 0; i < n; i++) {
            capacity[i] -= rocks[i];
        }

        Arrays.sort(capacity);
        
        int count = 0;
        for (int i = 0; i < n && additionalRocks > 0; i++) {
            if (capacity[i] == 0) {
                count++;
            } else if (additionalRocks >= capacity[i]) {
                additionalRocks -= capacity[i];
                count++;
            } else {
                break;
            }
        }

        return count;
    }
}
  • Runtime: 13 ms (Beats: 98.24%)
  • Memory: 54.91 MB (Beats: 85.92%)