Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 662 Bytes

1710.-maximum-units-on-a-truck.md

File metadata and controls

21 lines (19 loc) · 662 Bytes

1710. Maximum Units on a Truck

class Solution:
    def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
        boxTypes = sorted(boxTypes, key = lambda x :x[1],reverse = True)
        # print(boxTypes)
        idx = 0
        ans = 0
        while truckSize > 0 and idx < len(boxTypes):
            # print(truckSize,idx)
            if truckSize - boxTypes[idx][0] >= 0:
                ans += boxTypes[idx][0] * boxTypes[idx][1]
                truckSize -= boxTypes[idx][0]
                idx += 1
            else: 
                ans += truckSize * boxTypes[idx][1]
                truckSize = 0
        return ans