[Java] ✅ 22ms ✅ 99% ✅ Priority Queue ✅ CLEAN CODE
Approach\n1. Use 2 queues of type int[]. \n    - buyQueue, sorting in descending order of price\n    - sellQueue, sorting in ascending order of price\n2. Proces
# Approach\n1. Use 2 queues of type int[]. \n    - buyQueue, sorting in descending order of price\n    - sellQueue, sorting in ascending order of price\n2. Process each order by type:\n3. BUY, you need to look in sellQueue, \n    - loop while order\'s price >= head of queue price. \n    - Keep track of what you take out from sellQueue. \n    - Also, if the order ammount is still > 0, put it in the sellQueue\n4. SELL: you need to look in buyQueue;\n    - loop while queue is not empty and order price <= head of queue price\n    - Keep track of what you take out from buyQueue. If current head queue.amount == 0, remove from queue.\n    - At the end, if order\'s amount > 0, put it in the sellQueue\n5. For each step, you will have a delta (diff between what goes out from lookInqueue what goes in putInQueue)\n    - Add these deltas and return their modulo sum.\n\n# Complexity\n- Time complexity:$$O(n * log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<int[]> buyQueue = new PriorityQueue<>((a,b) -> Integer.compare(b[0], a[0]));\n        PriorityQueue<int[]> sellQueue = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));\n        long backlogStatus = 0L;\n        for (int[] order : orders) {\n            if (order[2] == 0) { //BUY\n                backlogStatus += processItem(order, sellQueue, buyQueue, 0);\n            } else { // SELL\n                backlogStatus += processItem(order, buyQueue, sellQueue, 1);\n            }\n        }\n        return (int)(backlogStatus % 1_000_000_007);\n    }\n    private int processItem(int[] order, PriorityQueue<int[]> lookInQueue, PriorityQueue<int[]> putInQueue, int op) {\n        if (lookInQueue.isEmpty() || !(op == 0 ? order[0] >= lookInQueue.peek()[0] : order[0] <= lookInQueue.peek()[0])) {\n            putInQueue.add(order);\n            return order[1];\n        }\n        int backlogDelta = 0;\n        while (!lookInQueue.isEmpty() && order[1] > 0 && (op == 0 ? order[0] >= lookInQueue.peek()[0] : order[0] <= lookInQueue.peek()[0])) {\n            int amountToRemove = Math.min(lookInQueue.peek()[1], order[1]);\n            backlogDelta += -amountToRemove;\n            order[1] -= amountToRemove;\n            lookInQueue.peek()[1] -= amountToRemove;\n            if (lookInQueue.peek()[1] == 0) {\n                lookInQueue.poll();\n            }\n        }\n        if (order[1] > 0) {\n            backlogDelta += order[1];\n            putInQueue.add(order);\n        }\n        return backlogDelta;\n    }\n}\n```
['Java']