-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1564_max_boxes_in_warehouse.cc
49 lines (46 loc) · 1.09 KB
/
1564_max_boxes_in_warehouse.cc
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
43
44
45
46
47
48
49
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
using namespace::std;
class Solution {
public:
int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
int cnt = 0;
int n = warehouse.size(), m = boxes.size();
sort(boxes.begin(), boxes.end());
stack<int> minheap;
stack<int> pos;
minheap.push(warehouse[0]);
pos.push(n - 1);
for (int i = 1; i < n; ++i) {
if (warehouse[i] < minheap.top()) {
minheap.push(warehouse[i]);
pos.push(n - 1 - i);
}
}
int i = 0, j = 0;
while (!minheap.empty()) {
int minhigh = minheap.top();
int minid = pos.top();
minheap.pop();
pos.pop();
while (i <= minid && j < m && boxes[j] <= minhigh) {
++cnt;
++i;
++j;
}
i = minid + 1;
}
return cnt;
}
};
int main()
{
return 0;
}