Browse Source

Add 1710.maximum-units-on-a-truck.py

master
Jaeha Choi 1 month ago
parent
commit
813ec7654a
Signed by: Jaeha.Choi GPG Key ID: FC8A150CF6C09314
  1. 19
      leetcode/1700-1799/1710.maximum-units-on-a-truck.py

19
leetcode/1700-1799/1710.maximum-units-on-a-truck.py

@ -0,0 +1,19 @@
from typing import List
class Solution:
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
boxTypes.sort(reverse=True, key=lambda x: x[1])
space = truckSize
acc = 0
for box in boxTypes:
if space - box[0] >= 0:
space -= box[0]
acc += box[0] * box[1]
else:
acc += box[1] * space
break
return acc
# Time: O(n log n)
# Space: O(1)
Loading…
Cancel
Save