-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1801.cpp
52 lines (52 loc) · 1.21 KB
/
1801.cpp
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
50
51
52
class Solution {
public:
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
priority_queue<array<int, 2>> buy;
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>>
sell;
for (auto& order : orders) {
auto p = order[0], a = order[1], o = order[2];
if (o == 0) {
while (!sell.empty() && a) {
auto [ps, as] = sell.top();
if (ps > p) break;
sell.pop();
if (a >= as) {
a -= as;
} else {
sell.push({ps, as - a});
a = 0;
}
}
if (a) {
buy.push({p, a});
}
} else {
while (!buy.empty() && a) {
auto [pb, ab] = buy.top();
if (pb < p) break;
buy.pop();
if (a >= ab) {
a -= ab;
} else {
buy.push({pb, ab - a});
a = 0;
}
}
if (a) {
sell.push({p, a});
}
}
}
int res = 0, mod = 1e9 + 7;
while (!buy.empty()) {
res = (res + buy.top()[1]) % mod;
buy.pop();
}
while (!sell.empty()) {
res = (res + sell.top()[1]) % mod;
sell.pop();
}
return res;
}
};