Leetcode 1829 Solution

This article provides solution to leetcode question 1829 (maximum-units-on-a-truck)

https://leetcode.com/problems/maximum-units-on-a-truck

Solution

class Solution: def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int: boxTypes = sorted(boxTypes, key = lambda x: x[1], reverse=True)
i = 0 ans = 0 while truckSize > 0 and i < len(boxTypes): box_num, unit_num = boxTypes[i]
target_num = min(truckSize, box_num)
truckSize -= target_num ans += target_num * unit_num
i += 1
return ans