Followable Java Code | PriorityQueue | Custom Order class
Intuition\nHeapify the input orders. As you add them to your buy and sell prioirty queues, see if you can make a trade. \n\nPS: The Order class basically helps 
# Intuition\nHeapify the input orders. As you add them to your buy and sell prioirty queues, see if you can make a trade. \n\nPS: The Order class basically helps to follow along and avoid messy code (demonstrates a little OOP too :P) \n\n# Approach\nUncomment all Sop\'s to follow along. Hope this helps!\n\n# Code\n```\nclass Order {\n    int price;\n    int amount;\n    Order(int p, int a) {\n        this.price = p;\n        this.amount = a;\n    }\n}\n\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        Queue<Order> buyQ = new PriorityQueue<Order>((o1, o2) -> (Integer.compare(o2.price, o1.price)));\n        Queue<Order> sellQ = new PriorityQueue<Order>((o1, o2) -> (Integer.compare(o1.price, o2.price)));\n        \n        // int i=1;\n        for (int[] order : orders) {\n            int priceI = order[0];\n            int amountI = order[1];\n            int typeI = order[2];\n            Order o = new Order(priceI, amountI);\n            if (typeI == 0) { // buy\n                buyQ.add(o);\n            } else if (typeI == 1) { // sell\n                sellQ.add(o);\n            }\n\n            // System.out.println("Starting processing order " + i);\n            processOrder(buyQ, sellQ);\n            // System.out.println("Ending processing order " + i+"\\n\\n");\n            // i +=1;\n            \n        }\n        int retval = 0;\n        for (Order o : buyQ) {\n            retval += o.amount;\n            retval %= Math.pow(10,9) + 7;\n            // System.out.println(retval);\n        }\n        for (Order o : sellQ) {\n            retval += o.amount;\n            retval %= Math.pow(10,9) + 7;\n            // System.out.println(retval);\n        }\n        \n        // retval %= Math.pow(10,9) + 7;\n        return retval;\n    }\n\n    private void processOrder(Queue<Order> b, Queue<Order> s) {\n        // System.out.format("Buy size %d, sell size %d\\n", b.size(), s.size());\n        \n        if (b.size() == 0 || s.size() == 0) return;\n        Order bOrder = b.peek();\n        Order sOrder = s.peek();\n        if (sOrder.price <= bOrder.price) {\n            // System.out.println("Trade happening");\n            if (bOrder.amount <= sOrder.amount) {\n                // System.out.format("Selling %d orders at price %d from %d orders\\n", bOrder.amount, sOrder.price, sOrder.amount);\n                b.poll();\n                sOrder.amount -= bOrder.amount;\n            } else {\n                // System.out.format("Selling %d orders at price %d from %d orders\\n", sOrder.amount, sOrder.price, bOrder.amount);\n                s.poll();\n                bOrder.amount -= sOrder.amount;\n            }\n            processOrder(b, s);\n        }\n    }\n}\n```
['Heap (Priority Queue)', 'Java']