File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Design/1801.Number-of-Orders-in-the-Backlog Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ typedef pair<long ,long > PII; // {price,amount}
2+
3+ class Solution {
4+ public:
5+ int getNumberOfBacklogOrders (vector<vector<int >>& orders)
6+ {
7+ priority_queue<PII>buy;
8+ priority_queue<PII, vector<PII>, greater<>>sell;
9+
10+ long ret = 0 ;
11+ long M = 1e9 +7 ;
12+
13+ for (auto order: orders)
14+ {
15+ ret = (ret+order[1 ]) % M;
16+
17+ if (order[2 ]==0 )
18+ {
19+ while (order[1 ]>0 && !sell.empty () && sell.top ().first <= order[0 ])
20+ {
21+ auto [price, amount] = sell.top ();
22+ sell.pop ();
23+ long num = min (amount, (long )order[1 ]);
24+ amount-=num;
25+ order[1 ]-=num;
26+ ret = (ret - num*2 + M) % M;
27+ if (amount > 0 )
28+ sell.push ({price, amount});
29+ }
30+ if (order[1 ] > 0 )
31+ buy.push ({order[0 ], order[1 ]});
32+ }
33+ else
34+ {
35+ while (order[1 ]>0 && !buy.empty () && buy.top ().first >= order[0 ])
36+ {
37+ auto [price, amount] = buy.top ();
38+ buy.pop ();
39+ long num = min (amount, (long )order[1 ]);
40+ amount-=num;
41+ order[1 ]-=num;
42+ ret = (ret - num*2 + M) % M;
43+ if (amount > 0 )
44+ buy.push ({price, amount});
45+ }
46+ if (order[1 ] > 0 )
47+ sell.push ({order[0 ], order[1 ]});
48+ }
49+ }
50+
51+ return ret;
52+ }
53+ };
You can’t perform that action at this time.
0 commit comments