Skip to content

Commit d32ec85

Browse files
authored
Create put-boxes-into-the-warehouse-ii.cpp
1 parent f9ef8b5 commit d32ec85

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
7+
sort(begin(boxes), end(boxes), greater<int>());
8+
int left = 0, right = size(warehouse) - 1;
9+
for (const auto& h : boxes) {
10+
if (h <= warehouse[left]) {
11+
++left;
12+
} else if (h <= warehouse[right]) {
13+
--right;
14+
}
15+
if (left > right) {
16+
break;
17+
}
18+
}
19+
return left + (size(warehouse) - 1 - right);
20+
}
21+
};

0 commit comments

Comments
 (0)