-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1564.cpp
More file actions
26 lines (26 loc) · 747 Bytes
/
1564.cpp
File metadata and controls
26 lines (26 loc) · 747 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
class Solution {
public:
int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
int n = warehouse.size();
int minHeight = INT_MAX;
for (int i = 0; i < n; ++i) {
minHeight = min(minHeight, warehouse[i]);
warehouse[i] = minHeight;
}
sort(boxes.begin(), boxes.end(), greater<int>());
int res = 0;
int boxIdx = boxes.size() - 1;
int houseIdx = n - 1;
while (boxIdx >= 0 && houseIdx >= 0) {
if (warehouse[houseIdx] >= boxes[boxIdx]) {
res++;
houseIdx--;
boxIdx--;
}
else {
houseIdx--;
}
}
return res;
}
};