-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1710-max_units_on_a_truck.py
33 lines (28 loc) · 1.06 KB
/
1710-max_units_on_a_truck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
https://leetcode.com/problems/maximum-units-on-a-truck/
Strat:
Sort & greedy
Stats: O(n lg n) time, O(n) / linear space
Runtime: 128 ms, faster than 79.47% of Python online submissions for Maximum Units on a Truck.
Memory Usage: 13.9 MB, less than 86.31% of Python online submissions for Maximum Units on a Truck.
"""
class Solution(object):
def maximumUnits(self, boxTypes, truckSize):
"""
:type boxTypes: List[List[int]]
:type truckSize: int
:rtype: int
"""
boxTypes = sorted(boxTypes, key=lambda x : x[1], reverse=True)
units_fitted = 0
space_left = truckSize
for box in boxTypes:
num_of_boxes = box[0]
num_of_units_in_box = box[1]
if space_left > num_of_boxes:
units_fitted += num_of_boxes * num_of_units_in_box
space_left -= num_of_boxes
else:
units_fitted += space_left * num_of_units_in_box
break
return units_fitted