forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
51 lines (51 loc) · 1.51 KB
/
Solution.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
class Solution {
public:
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> sell;
priority_queue<pii> buy;
for (auto& e : orders) {
int p = e[0], a = e[1], t = e[2];
if (t == 0) {
while (a && !sell.empty() && sell.top().first <= p) {
auto [x, y] = sell.top();
sell.pop();
if (a >= y) {
a -= y;
} else {
sell.push({x, y - a});
a = 0;
}
}
if (a) {
buy.push({p, a});
}
} else {
while (a && !buy.empty() && buy.top().first >= p) {
auto [x, y] = buy.top();
buy.pop();
if (a >= y) {
a -= y;
} else {
buy.push({x, y - a});
a = 0;
}
}
if (a) {
sell.push({p, a});
}
}
}
long ans = 0;
while (!buy.empty()) {
ans += buy.top().second;
buy.pop();
}
while (!sell.empty()) {
ans += sell.top().second;
sell.pop();
}
const int mod = 1e9 + 7;
return ans % mod;
}
};