Java/Python Heap Solution
If you got WA, you might need to see the following requirements:\nIf the order is a buy order, you look at the sell order with the smallest price in the backlog
If you got WA, you might need to see the following requirements:\n**If the order is a buy order, you look at the sell order with the smallest price in the backlog.**\n**if the order is a sell order, you look at the buy order with the largest price in the backlog**\n\n**Please upvote for this if you find it is helpful!**\n\n**Java:**\n\n```\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) { // processing buy orders:\n                    while (!sell.isEmpty() && o[0] >= sell.peek()[0] && o[1] >= sell.peek()[1]) {\n                        o[1] -= sell.peek()[1];\n                        sell.poll();\n                    }\n                    if (!sell.isEmpty() && o[0] >= sell.peek()[0] && o[1] > 0) {\n                        sell.peek()[1] -= o[1];\n                        o[1] = 0;\n                    }\n                    if (o[1] > 0) {\n                        buy.offer(o);\n                    }\n            } else { // processing sell orders:\n                    while (!buy.isEmpty() && o[0] <= buy.peek()[0] && o[1] >= buy.peek()[1]) {\n                        o[1] -= buy.peek()[1];\n                        buy.poll();\n                    }\n                    if (!buy.isEmpty() && o[0] <= buy.peek()[0] && o[1] > 0) {\n                        buy.peek()[1] -= o[1];\n                        o[1] = 0;\n                    }\n                    if (o[1] > 0) {\n                        sell.offer(o);\n                    }\n                }                \n        }\n        long res = 0;\n        for (int[] o : sell) {\n            res += o[1];\n        }\n        for (int[] o : buy) {\n            res += o[1];\n        }\n        return (int)(res % 1000000007);\n    }\n```\n\n**Python:**\n\n```\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        buy = []\n        sell = []\n        for price, amount, orderType in orders :\n            if orderType == 0 :\n                # buy\n                while amount > 0 and len(sell) > 0 :\n                    pt = sell[0]\n                    if pt[0] > price :\n                        break\n                    deal = min(amount, pt[1])\n                    amount -= deal\n                    pt[1] -= deal\n                    if pt[1] == 0 :\n                        heapq.heappop(sell)\n                if amount > 0 :\n                    heapq.heappush(buy, [-price, amount])\n            else :\n                # sell\n                while amount > 0 and len(buy) > 0 :\n                    pt = buy[0]\n                    if -pt[0] < price :\n                        break\n                    deal = min(amount, pt[1])\n                    amount -= deal\n                    pt[1] -= deal\n                    if pt[1] == 0 :\n                        heapq.heappop(buy)\n                if amount > 0 :\n                    heapq.heappush(sell, [price, amount])\n        res = sum([t[1] for t in buy]) + sum([t[1] for t in sell])\n        return res % (10**9+7)\n```\n
[]