Leetcode 1829 Solution
This article provides solution to leetcode question 1829 (maximum-units-on-a-truck)
Access this page by simply typing in "lcs 1829" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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