Java | Priority Queue
\n\n# Code\n\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> 
\n\n# Code\n```\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> b[0] - a[0]);\n        PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);\n        long mod = 1000000007;\n        int res = 0;\n\n        for (int[] order : orders) {\n            if (order[2] == 0) {\n                while (!minHeap.isEmpty()\n                        && minHeap.peek()[0] <= order[0]\n                        && order[1] > 0) {\n                    int min = Math.min(minHeap.peek()[1], order[1]);\n                    order[1] -= min;\n                    minHeap.peek()[1] -= min;\n                    if (minHeap.peek()[1] == 0)\n                        minHeap.poll();\n                }\n\n                if (order[1] > 0) {\n                    maxHeap.offer(new int[] { order[0], order[1] });\n                }\n            } else {\n                while (!maxHeap.isEmpty()\n                        && maxHeap.peek()[0] >= order[0]\n                        && order[1] > 0) {\n                    int min = Math.min(maxHeap.peek()[1], order[1]);\n                    order[1] -= min;\n                    maxHeap.peek()[1] -= min;\n                    if (maxHeap.peek()[1] == 0)\n                        maxHeap.poll();\n                }\n\n                if (order[1] > 0) {\n                    minHeap.offer(new int[] { order[0], order[1] });\n                }\n            }\n        }\n\n        while (!minHeap.isEmpty()) {\n            res += minHeap.poll()[1];\n            res %= mod;\n        }\n\n        while (!maxHeap.isEmpty()) {\n            res += maxHeap.poll()[1];\n            res %= mod;\n        }\n\n        return res;\n    }\n}\n```
['Java']