Skip to content

Commit 64b6744

Browse files
authored
Create 1801.Number-of-Orders-in-the-Backlog.cpp
1 parent c793aa5 commit 64b6744

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
typedef pair<long,long> PII;
2+
class Solution {
3+
public:
4+
int getNumberOfBacklogOrders(vector<vector<int>>& orders)
5+
{
6+
priority_queue<PII>buy;
7+
priority_queue<PII, vector<PII>, greater<>>sell;
8+
9+
long ret = 0;
10+
long M = 1e9+7;
11+
for (auto order: orders)
12+
{
13+
ret = (ret+order[1])%M;
14+
15+
if (order[2]==0)
16+
{
17+
while (!sell.empty() && order[1]>0 && sell.top().first <= order[0])
18+
{
19+
auto [price, amount] = sell.top();
20+
sell.pop();
21+
long num = min(amount, (long)order[1]);
22+
amount -= num;
23+
order[1] -= num;
24+
ret = (ret-num*2+M)%M;
25+
if (amount >0)
26+
sell.push({price, amount});
27+
}
28+
if (order[1]>0)
29+
buy.push({order[0], order[1]});
30+
}
31+
else
32+
{
33+
while (!buy.empty() && order[1]>0 && buy.top().first >= order[0])
34+
{
35+
auto [price, amount] = buy.top();
36+
buy.pop();
37+
long num = min(amount, (long)order[1]);
38+
amount -= num;
39+
order[1] -= num;
40+
ret = (ret-num*2+M)%M;
41+
if (amount >0)
42+
buy.push({price, amount});
43+
}
44+
if (order[1]>0)
45+
sell.push({order[0], order[1]});
46+
}
47+
}
48+
49+
return ret;
50+
51+
}
52+
};

0 commit comments

Comments
 (0)