Skip to content

Commit 3aef8c8

Browse files
authored
Create maximum-units-on-a-truck.py
1 parent d34be60 commit 3aef8c8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/maximum-units-on-a-truck.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(nlogn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maximumUnits(self, boxTypes, truckSize):
6+
"""
7+
:type boxTypes: List[List[int]]
8+
:type truckSize: int
9+
:rtype: int
10+
"""
11+
boxTypes.sort(key=lambda x: x[1], reverse=True)
12+
result = 0
13+
for box, units in boxTypes:
14+
if truckSize > box:
15+
truckSize -= box
16+
result += box*units
17+
else:
18+
result += truckSize*units
19+
break
20+
return result

0 commit comments

Comments
 (0)