-
Notifications
You must be signed in to change notification settings - Fork 106
/
put-boxes-into-the-warehouse-i.py
42 lines (39 loc) · 1.04 KB
/
put-boxes-into-the-warehouse-i.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
34
35
36
37
38
39
40
41
42
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def maxBoxesInWarehouse(self, boxes, warehouse):
"""
:type boxes: List[int]
:type warehouse: List[int]
:rtype: int
"""
boxes.sort(reverse=True)
result = 0
for h in boxes:
if h > warehouse[result]:
continue
result += 1
if result == len(warehouse):
break
return result
# Time: O(nlogn + m)
# Space: O(1)
class Solution2(object):
def maxBoxesInWarehouse(self, boxes, warehouse):
"""
:type boxes: List[int]
:type warehouse: List[int]
:rtype: int
"""
boxes.sort()
for i in xrange(1, len(warehouse)):
warehouse[i] = min(warehouse[i], warehouse[i-1])
result, curr = 0, 0
for h in reversed(warehouse):
if boxes[curr] > h:
continue
result += 1
curr += 1
if curr == len(boxes):
break
return result