[Java/C++/Python] Priority Queue
Complexity\nTime O(nlogn)\nSpace O(n)\n\n\nJava\njava\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<int[]> buy = new Priorit
# **Complexity**\nTime `O(nlogn)`\nSpace `O(n)`\n<br>\n\n**Java**\n```java\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<int[]> buy = new PriorityQueue<>((a, b) -> (b[0] - a[0]));\n        PriorityQueue<int[]> sell = new PriorityQueue<>((a, b) -> (a[0] - b[0]));\n        for (int[] o : orders) {\n            if (o[2] == 0)\n                buy.offer(o);\n            else\n                sell.offer(o);\n            while (!buy.isEmpty() && !sell.isEmpty() && sell.peek()[0] <= buy.peek()[0]) {\n                int k = Math.min(buy.peek()[1], sell.peek()[1]);\n                buy.peek()[1] -= k;\n                sell.peek()[1] -= k;\n                if (buy.peek()[1] == 0) buy.poll();\n                if (sell.peek()[1] == 0) sell.poll();\n            }\n\n        }\n        int res = 0, mod = 1000000007;\n        for (int[] o : sell)\n            res = (res + o[1]) % mod;\n        for (int[] o : buy)\n            res = (res + o[1]) % mod;\n        return res;\n    }\n```\n\n**C++**\n```cpp\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        priority_queue<vector<int>>buy;\n        priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>>sell;\n        for (auto& o : orders) {\n            if (o[2] == 0)\n                buy.push(o);\n            else\n                sell.push(o);\n            while (buy.size() && sell.size() && sell.top()[0] <= buy.top()[0]) {\n                int k = min(buy.top()[1], sell.top()[1]);\n                vector<int> tmp = buy.top(); buy.pop();\n                tmp[1] -= k;\n                if (tmp[1]) buy.push(tmp);\n\n                tmp = sell.top(); sell.pop();\n                tmp[1] -= k;\n                if (tmp[1]) sell.push(tmp);\n            }\n\n        }\n        int res = 0, mod = 1e9 + 7;\n        while (sell.size())\n            res = (res + sell.top()[1]) % mod, sell.pop();\n        while (buy.size())\n            res = (res + buy.top()[1]) % mod, buy.pop();\n        return res;\n    }\n```\n\n**Python**\n```py\n    def getNumberOfBacklogOrders(self, orders):\n        sell, buy = [], []\n        for p, a, t in orders:\n            if t == 0:\n                heapq.heappush(buy, [-p, a])\n            else:\n                heapq.heappush(sell, [p, a])\n            while sell and buy and sell[0][0] <= -buy[0][0]:\n                k = min(buy[0][1], sell[0][1])\n                buy[0][1] -= k\n                sell[0][1] -= k\n                if buy[0][1] == 0: heapq.heappop(buy)\n                if sell[0][1] == 0: heapq.heappop(sell)\n        return sum(a for p, a in buy + sell) % (10**9 + 7)\n```\n
[]