C++ priority queue
Intuition\n Describe your first thoughts on how to solve this problem. \nThe requirement is to fetch the largest existing buy orders or the smallest existing se
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe requirement is to fetch the largest existing buy orders or the smallest existing sell orders. We need to be able to retrieve buy or sell orders in a sorted manner. A priority_queue/heap helps with this.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCreate a max heap called `buy_queue` which stores a `(price, amount)` pair.\nCreate a min heap called `sell_queue` which stores a `(price, amount)` pair.\nWhenever we have a buy , pop from the sell queue until the price at the top of the heap is greater than the price on the order or the amount of orders at index i is 0.\nIf there are any remaining orders, push them to the buy_queue.\n\nSimilarly for the sell orders.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(N*log(N))$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(N)$$\n\n# Code\n```\nclass Solution {\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        int mod = 1000000007;\n        struct pairComparatorLess {\n            bool operator()(const pair<int,int>& a, const pair<int, int>&b) {\n                return a.first < b.first;\n            }\n        };\n       \n        struct pairComparatorGreater {\n            bool operator()(const pair<int,int>& a, const pair<int, int>&b) {\n                return b.first < a.first;\n            }\n        };\n        priority_queue<pair<int, int>, vector<pair<int, int>>, pairComparatorLess> buy_queue;\n        priority_queue<pair<int, int> , vector<pair<int, int>>, pairComparatorGreater> sell_queue;\n\n        for(vector<int>& order: orders) {\n            int price = order[0], amount = order[1], typ = order[2];\n            switch (typ) {\n                // buy\n                case 0:\n                while(!sell_queue.empty() && sell_queue.top().first <= price) {\n                    auto [sp, amt] = sell_queue.top();\n                    sell_queue.pop();\n                    int x = amount;\n                    amount -= min(amount, amt);\n                    amt  -= min(x, amt);\n                    if (amt > 0) {\n                        sell_queue.push(make_pair(sp, amt));\n                        break;\n                    }\n                }\n                if (amount > 0) {\n                    buy_queue.push(make_pair(price, amount));\n                }\n                break;\n                //sell\n                case 1:\n                while(!buy_queue.empty() && buy_queue.top().first >= price && amount > 0) {\n                    auto [sp, amt] = buy_queue.top();\n                    buy_queue.pop();\n                    int x = amount;\n                    amount -= min(amount, amt);\n                    amt  -= min(x, amt);\n                    if (amt > 0) {\n                        buy_queue.push(make_pair(sp, amt));\n                        break;\n                    }\n                }\n                if (amount > 0) {\n                    sell_queue.push(make_pair(price, amount));\n                }\n                break;\n            }\n        }\n        int res = 0;\n        while(!buy_queue.empty()) { \n            auto [p, amt] = buy_queue.top();\n            buy_queue.pop();\n            res += amt % mod; res %= mod;\n        }\n        while(!sell_queue.empty()) { \n            auto [p, amt] = sell_queue.top();\n            sell_queue.pop();\n            res += amt % mod; res %= mod;\n        }\n        return res;\n    }\n};\n```
['C++']