-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1580.cpp
More file actions
31 lines (28 loc) · 937 Bytes
/
1580.cpp
File metadata and controls
31 lines (28 loc) · 937 Bytes
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
class Solution {
public:
int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
int n = warehouse.size();
vector<int> left(n, 0);
vector<int> right(n, 0);
left[0] = warehouse[0];
right[n - 1] = warehouse[n - 1];
for (int i = 1; i < n; ++i) {
left[i] = min(left[i - 1], warehouse[i]);
}
for (int i = n - 2; i >= 0; --i) {
right[i] = min(right[i + 1], warehouse[i]);
}
vector<int> heights(n, 0);
for (int i = 0; i < n; ++i) heights[i] = max(left[i], right[i]);
sort(boxes.begin(), boxes.end());
sort(heights.begin(), heights.end());
int index = 0;
for (int i = 0; i < n; ++i) {
if (heights[i] >= boxes[index]) {
index++;
}
if (index == boxes.size()) break;
}
return index;
}
};