Java Priority Queue with comments
\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        // create a max heap for buy orders sorted by price\n        // When we p
```\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        // create a max heap for buy orders sorted by price\n        // When we peak buy orders, we will get the largest price in buyOrders without removing it\n        PriorityQueue<int []> buyOrders = new PriorityQueue<>((a, b) -> b[0] - a[0]);\n        // create a min heap for sell orders sorted by price\n        // When we peak sellOrders, we will get the smallest price in sellOrders without removing it\n        PriorityQueue<int []> sellOrders = new PriorityQueue<>((a, b) -> a[0] - b[0]);\n        for (int [] order : orders) {\n            int price = order[0];\n            int amount = order[1];\n            int orderType = order[2];\n            if (orderType == 0) {\n                // buying power must be greater than or equal to selling price\n                // Hence, price >= sellOrders.peek()[0]\n                // added amount > 0 in the condition to avoid infinite loop \n                while (!sellOrders.isEmpty() && price >= sellOrders.peek()[0] && amount > 0) {\n                    // \n                    if (amount >= sellOrders.peek()[1]) {\n                        amount -= sellOrders.peek()[1];\n                        sellOrders.poll();\n                    } else {\n                        sellOrders.peek()[1] -= amount;\n                        amount = 0;\n                    }\n                    \n                }\n                // if the amount is a positive number, add the order to buyOrders PriorityQueue\n                if (amount > 0) {\n                   buyOrders.add(new int [] {price, amount});\n                }\n            } else {\n                // Same thing there. Buying power must be greater than or equal to selling price\n                // Hence, price <= buyOrders.peek()[0] since the orderType is 1 (sell order) here\n                while (!buyOrders.isEmpty() && price <= buyOrders.peek()[0] && amount > 0) {\n                    if (amount >= buyOrders.peek()[1]) {\n                        amount -= buyOrders.peek()[1];\n                        buyOrders.poll();\n                    } else {\n                        buyOrders.peek()[1] -= amount;\n                        // set amount to zero after substracting \n                        amount = 0;\n                    }\n                   \n                }\n                // if the amount is a positive number, add the order to sellOrders PriorityQueue\n                if (amount > 0) {\n                  sellOrders.add(new int [] {price, amount});\n                }\n            }\n        }\n        \n        long sum = 0;\n        int mod = 1_000_000_007;\n        // ensure the sum stays withint the int range\n        while (!buyOrders.isEmpty()) {\n            sum = (sum + buyOrders.poll()[1]) % mod;\n        }\n        while (!sellOrders.isEmpty()) {\n            sum = (sum + sellOrders.poll()[1]) % mod;\n        }\n\n        \n        return (int) sum;\n        \n    }\n}\n```
['Java']