Java Solution with Priority Queue
\nclass Solution {\n    static int mod = 1000000007;\n    \n    public int getNumberOfBacklogOrders(int[][] orders) {\n        Queue<int[]> buyBacklog = new Pri
```\nclass Solution {\n    static int mod = 1000000007;\n    \n    public int getNumberOfBacklogOrders(int[][] orders) {\n        Queue<int[]> buyBacklog = new PriorityQueue<>((a,b) -> b[0]-a[0]);\n        Queue<int[]> sellBacklog = new PriorityQueue<>((a,b) -> a[0]-b[0]);\n        \n        \n        \n        for(int[] order: orders) {\n            if(order[2] == 0) { // buy\n                while (!sellBacklog.isEmpty() && sellBacklog.peek()[0] <= order[0]) {\n                    int [] minSell = sellBacklog.peek();\n                    \n                    if(minSell[1] > order[1]) {\n                        // more to sell than buy\n                        minSell[1] -= order[1];\n                        order[1] = 0;\n                        break;\n                    } else if(minSell[1] == order[1]) {\n                        // buy quantity and sell quantity are same\n                        sellBacklog.remove();\n                        order[1] = 0;\n                        break;\n                    } else{\n                        // buy more quantity than this batch has to sell\n                        order[1] -= minSell[1];\n                        sellBacklog.remove();\n                    }\n                } // while\n                if(order[1] > 0) {\n                    buyBacklog.add(order);\n                }\n            } else { // sell\n                while(!buyBacklog.isEmpty() && buyBacklog.peek()[0] >= order[0]) {\n                    int[] maxBuy = buyBacklog.peek();\n                    \n                    if(maxBuy[1] > order[1]) {\n                        // more buy than sell\n                        maxBuy[1] -= order[1];\n                        order[1] = 0;\n                        break;\n                    } else if (maxBuy[1] == order[1]) {\n                        // equal buy and sell\n                        order[1] = 0;\n                        buyBacklog.remove();\n                        break;\n                    } else {\n                        // less to buy than sell\n                        order[1] -= maxBuy[1];\n                        buyBacklog.remove();\n                    }\n                } // while\n                if(order[1] > 0) {\n                    sellBacklog.add(order);\n                }\n            }\n        } // for\n        long batch = 0;\n        \n        for(int[] buy: buyBacklog) batch = (batch+buy[1])%mod;\n        for(int[] sell: sellBacklog) batch = (batch+sell[1])%mod;\n        \n        return (int) batch;\n    }\n}\n```
[]