Java | Priority Queue
\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<BO> pqBo = new PriorityQueue(new BOComp());\n        Priori
```\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<BO> pqBo = new PriorityQueue(new BOComp());\n        PriorityQueue<SO> pqSo = new PriorityQueue(new SOComp());\n        // IF BUY CHECK SMALLEST SELL, SELL SHOULD BE LESS\n        // IF SELL CHECK LARGEST BUY, BUY SHOULD BE MORE\n        for(int[] item : orders){\n            boolean isBo = false;\n            BO buyBlog = null;\n            SO sellBlog = null;\n            if(item[2] == 0){\n                buyBlog = new BO(item[0], item[1]);\n                if(pqSo.peek() == null){\n                     pqBo.add(buyBlog);\n                }else{\n                    sellBlog = pqSo.poll();\n                }\n                isBo = true;\n            }else{\n                sellBlog = new SO(item[0], item[1]);\n                if(pqBo.peek() == null){\n                     pqSo.add(sellBlog);\n                }else{\n                    buyBlog = pqBo.poll();\n                }\n            }\n            if(buyBlog == null || sellBlog == null) continue;\n            if(isBo == true){\n                if(sellBlog.getPrice() <= buyBlog.getPrice()){\n                    if(sellBlog.getQty() >= buyBlog.getQty()){\n                        sellBlog.setQty(sellBlog.getQty() - buyBlog.getQty());\n                        if(sellBlog.getQty() > 0){\n                            pqSo.add(sellBlog);\n                        }\n                    }else{\n                        buyBlog.setQty(buyBlog.getQty() - sellBlog.getQty());\n                        while(buyBlog.getQty() > 0 && pqSo.peek() != null && pqSo.peek().getPrice() <= buyBlog.getPrice()){\n                            if(buyBlog.getQty() > pqSo.peek().getQty()){\n                                buyBlog.setQty(buyBlog.getQty() - pqSo.poll().getQty());\n                            }else{\n                                pqSo.peek().setQty(pqSo.peek().getQty() - buyBlog.getQty() );\n                                buyBlog.setQty(0);\n                            }\n                            \n                        }\n                        if(buyBlog.getQty() > 0){\n                            pqBo.add(buyBlog);\n                        }\n                    }\n                }else{\n                    pqSo.add(sellBlog);\n                    pqBo.add(buyBlog);\n                }\n            }else{\n                \n                if(sellBlog.getPrice() <= buyBlog.getPrice()){\n                    if(sellBlog.getQty() >= buyBlog.getQty()){\n                        sellBlog.setQty(sellBlog.getQty() - buyBlog.getQty());\n                        \n                        while(sellBlog.getQty() > 0 && pqBo.peek() != null && pqBo.peek().getPrice() >= sellBlog.getPrice()){\n                            \n                            if(sellBlog.getQty() > pqBo.peek().getQty()){\n                                sellBlog.setQty(sellBlog.getQty() - pqBo.poll().getQty());\n                            }else{\n                                pqBo.peek().setQty(pqBo.peek().getQty() - sellBlog.getQty() );\n                                sellBlog.setQty(0);\n                            }\n                            \n                        }\n                        \n                        if(sellBlog.getQty() > 0){\n                            pqSo.add(sellBlog);\n                        }\n                    }else{\n                        buyBlog.setQty(buyBlog.getQty() - sellBlog.getQty());\n                        if(buyBlog.getQty() > 0){\n                            pqBo.add(buyBlog);\n                        }\n                    }\n                }else{\n                    pqBo.add(buyBlog);\n                    pqSo.add(sellBlog);\n                }\n            }\n        }\n        BO buyBlog = null;\n        SO sellBlog = null;\n        int blogQty = 0;\n        int max = (int )(Math.pow(10,9) + 7);\n        while(!pqBo.isEmpty()){\n            buyBlog = pqBo.poll();\n            blogQty = (blogQty + buyBlog.getQty()) % max;\n        }\n        while(!pqSo.isEmpty()){\n            sellBlog = pqSo.poll();\n            blogQty = (blogQty + sellBlog.getQty()) % max;\n        }\n        return blogQty;\n    }\n    \n}\n\nclass BO {\n    private int price;\n    private int qty;\n    public BO(int price, int qty){\n        this.price = price;\n        this.qty = qty;\n    }\n    public int getPrice(){\n        return this.price;\n    }\n    public int getQty(){\n        return this.qty;\n    }\n    public void setQty(int qty){\n        this.qty = qty;\n    }\n    @Override\n    public String toString(){\n        return "Price is " + this.getPrice() + " Qty is " + this.getQty();\n    }\n}\n\nclass SO {\n    private int price;\n    private int qty;\n    public SO(int price, int qty){\n        this.price = price;\n        this.qty = qty;\n    }\n    public int getPrice(){\n        return this.price;\n    }\n    public int getQty(){\n        return this.qty;\n    }\n    public void setQty(int qty){\n        this.qty = qty;\n    }\n    @Override\n    public String toString(){\n        return "Price is " + this.getPrice() + " Qty is " + this.getQty();\n    }\n}\n\nclass BOComp implements Comparator<BO>{\n    public int compare(BO itemA,BO itemB){\n        return itemA.getPrice() < itemB.getPrice()? 1: -1;\n    }\n}\n\nclass SOComp implements Comparator<SO>{\n    public int compare(SO itemA,SO itemB){\n        return itemA.getPrice() > itemB.getPrice()? 1: -1;\n    }\n}\n```
[]