[C++] Priority Queue Solution
The problem description is really confusing.\n\nIntuition:\n\nDo what the description states.\n\nUsing a priority queue to store buy and sell respectively, sell
The problem description is really confusing.\n\n**Intuition**:\n\nDo what the description states.\n\nUsing a priority queue to store buy and sell respectively, sell sorted from smallest to largest, buy sorted from largest to smallest.\n\nA very important insight is that do not store individual orders, instead, store the **number of orders** in that particular amount in total.\n\nThen every time grab the top element and compare.\n\n```\nclass Solution {\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        // value, amount (buy smallest in sell, sell largest in buy), sell sorted from smallest to largest, buy sorted from largest to smallest\n        long ans = 0;\n        priority_queue<pair<int,long>, vector<pair<int,long>>, greater<pair<int,long>>> sell;\n        priority_queue<pair<int,long>> buy;\n        for(vector<int> order:orders){\n            if(order[2]){\n                if(buy.empty() || buy.top().first < order[0]){\n                    sell.push(make_pair(order[0],order[1]));\n                    continue;\n                }\n                while(!buy.empty() && order[1] > 0 && buy.top().first >= order[0]){\n                    if(buy.top().second >= order[1]){\n                        pair<int,long> cur = buy.top();\n                        cur.second -= order[1];\n                        buy.pop();\n                        buy.push(cur);\n                        order[1] = 0;\n                        break;\n                    }\n                    order[1] -= buy.top().second;\n                    buy.pop();\n                }\n                if(order[1] > 0) sell.push(make_pair(order[0],order[1]));\n            } else{\n                if(sell.empty() || sell.top().first > order[0]){\n                    buy.push(make_pair(order[0],order[1]));\n                    continue;\n                }\n                while(!sell.empty() && order[1] > 0 && sell.top().first <= order[0]){\n                    if(sell.top().second >= order[1]){\n                        pair<int,long> cur = sell.top();\n                        cur.second -= order[1];\n                        sell.pop();\n                        sell.push(cur); \n                        order[1] = 0;\n                        break;\n                    }\n                    order[1] -= sell.top().second;\n                    sell.pop();\n                }\n                if(order[1] > 0) buy.push(make_pair(order[0],order[1]));\n            }\n        }\n        while(!sell.empty()){\n            ans += sell.top().second; sell.pop();\n        }\n        ans %= 1000000007;\n        while(!buy.empty()){\n            ans += buy.top().second; buy.pop();\n        }\n        return ans % 1000000007;\n    }\n};\n```
['C', 'Heap (Priority Queue)', 'C++']