Java Sol: Number of Orders in the Backlog
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nminHeap for sell.\nmaxHeap for buy.\n Describe your approach to solving t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nminHeap for sell.\nmaxHeap for buy.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    final static int MOD = (int) (1e9 + 7);\n    class Order{\n        int price;\n        int amount;\n        Order(int price, int amount){\n            this.price = price;\n            this.amount = amount;\n        }\n    }\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<Order> sell = new PriorityQueue<>((Order o1,Order o2)->{\n            return o1.price - o2.price;\n        });\n        PriorityQueue<Order> buy = new PriorityQueue<>((Order o1,Order o2)->{\n            return o2.price - o1.price;\n        });\n\n        boolean flag;\n        for(int[] ord : orders){\n            int amount = ord[1];\n            int price = ord[0];\n            if(ord[2] == 0){ //buy\n                while(amount>0 && !sell.isEmpty() && sell.peek().price<=price){\n                    Order front = sell.poll();\n                    if(front.amount>=amount){\n                        front.amount = front.amount - amount;\n                        amount = 0;\n                        if(front.amount>0)  sell.add(front);\n                    }    \n                    else\n                        amount = amount - front.amount;          \n                }\n                if(amount > 0)  buy.add(new Order(price, amount));\n            }else{ //sell\n                while(amount>0 && !buy.isEmpty() && buy.peek().price>=price){\n                    Order front = buy.poll();\n                    if(front.amount>=amount){\n                        front.amount = front.amount - amount;\n                        amount = 0;\n                        if(front.amount>0)  buy.add(front);\n                    }    \n                    else\n                        amount = amount - front.amount;         \n                }\n                if(amount > 0)  sell.add(new Order(price, amount));\n            }\n        }\n        long ans = 0;\n        while(!sell.isEmpty())\n            ans= (ans + sell.poll().amount) % MOD;\n        \n        while(!buy.isEmpty())\n            ans= (ans + buy.poll().amount) % MOD;\n\n        return (int) ans%MOD;\n        \n    }\n}\n```
['Java']