C++ solutions Two Heaps
\n// Please, upvote if you like it. Thanks :-)\n// TC - O(nlog(n))\n// SC - O(n)\ntypedef pair<int, int> pii;\nint getNumberOfBacklogOrders(vector<vector<int>>&
```\n// Please, upvote if you like it. Thanks :-)\n// TC - O(nlog(n))\n// SC - O(n)\ntypedef pair<int, int> pii;\nint getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n\tpriority_queue<pii> buy;\n\tpriority_queue<pii, vector<pii>, greater<pii>> sell;\n\tfor (const auto& order : orders) {\n\t\tint price = order[0], amount = order[1], type = order[2];\n\t\tif (type == 0) { // buy\n\t\t\twhile (amount > 0 && !sell.empty() && sell.top().first <= price) {\n\t\t\t\tauto [sell_price, sell_amount] = sell.top();\n\t\t\t\tsell.pop();\n\t\t\t\tif (sell_amount > amount) {\n\t\t\t\t\tsell.push( { sell_price, sell_amount - amount } );\n\t\t\t\t\tamount = 0;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tamount -= sell_amount;\n\t\t\t}\n\t\t\tif (amount > 0)\n\t\t\t\tbuy.push( { price, amount } );\n\t\t}\n\t\telse {\n\t\t\twhile (amount > 0 && !buy.empty() &&\n\t\t\t\t\tbuy.top().first >= price) {\n\t\t\t\tauto [buy_price, buy_amount] = buy.top();\n\t\t\t\tbuy.pop();\n\t\t\t\tif (buy_amount > amount) {\n\t\t\t\t\tbuy.push( { buy_price, buy_amount - amount } );\n\t\t\t\t\tamount = 0;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tamount -= buy_amount;\n\t\t\t}\n\t\t\tif (amount > 0)\n\t\t\t\tsell.push( { price, amount } );\n\t\t}\n\t}\n\tint res = 0, mod = 1e9+7;\n\twhile (!buy.empty()) {\n\t\tres = (res + buy.top().second) % mod;\n\t\tbuy.pop();\n\t}\n\twhile (!sell.empty()) {\n\t\tres = (res + sell.top().second) % mod;\n\t\tsell.pop();\n\t}\n\treturn res;\n}\n```
['Greedy', 'C', 'Heap (Priority Queue)', 'C++']