Skip to content
LC-2279 Medium LeetCode

2279. Maximum Bags With Full Capacity of Rocks

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 68% Topics: Array, Greedy, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

# sort, greedy
class Solution(object):
    def maximumBags(self, capacity, rocks, additionalRocks):
        """
        :type capacity: List[int]
        :type rocks: List[int]
        :type additionalRocks: int
        :rtype: int
        """
        for i in xrange(len(capacity)):
            capacity[i] -= rocks[i]
        capacity.sort()
        for i, c in enumerate(capacity):
            if c > additionalRocks:
                return i
            additionalRocks -= c
        return len(capacity)

Solution from kamyu104/LeetCode-Solutions · MIT