File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Priority_Queue/1801.Number-of-Orders-in-the-Backlog Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments