1710. Maximum Units on a Truck

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1710

class Solution:
    def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
        cnt = [0] * 1001
        for a, b in boxTypes:
            cnt[b] += a
        res = 0
        for b in range(1000, 0, -1):
            a = cnt[b]
            if a:
                res += b * min(truckSize, a)
                truckSize -= a
                if truckSize <= 0:
                    break
        return res