Using heap
Intuition\nWe need to maintain backlog of sellOrders in minimum price order and backlog of buyOrders in maximum price order. This implies the datastructure we u
# Intuition\nWe need to maintain backlog of sellOrders in minimum price order and backlog of buyOrders in maximum price order. This implies the datastructure we use must maintain records in sorted order and every time an order is executed completely, the next order must take its place. This is a good fit for Heap datastructure.\n\nOnce you figure this out, the logic for executing orders is a straightforward implementation of the requirement.\n\n# Approach\n1. Process orders in a loop\n2. Determine order type\n3. If buy order, keeping looking for sell order in minheap that matches criteria sellOrder.price<=buyOrder.price. If any buyOrders remain, add it to maxheap\n4. If sell order, keep looking for buy order in maxheap that matches criteria buyOrder.price>=sellOrder.price. If any sellOrders remain, add it to minheap\n5. After one pass through the orders, look for remaining orders in minheap and maxheap and compute their amount sum. This sum can easily exceed integer max length so store it in a long\n6. Return the sum%(10^9+7)\n\n\n# Complexity\n- Time complexity:\nO(n^2) - while looping through orders, we may need to trace back the heap to execute an order at every element\n\n- Space complexity:\nO(n) for maintaining heaps\n\n# Code\n```\nclass Order{\n    int price;\n    int amount;\n}\n\nclass SellOrder extends Order{\n    \n}\n\nclass BuyOrder extends Order{\n    \n}\n\nclass OrderComparator implements Comparator<Order>{\n    public int compare(Order a, Order b)\n    {\n        return a.price-b.price;\n    }\n}\n\nclass Solution {\n    PriorityQueue<Order> sellOrders = new PriorityQueue<>(new OrderComparator());\n    PriorityQueue<Order> buyOrders = new PriorityQueue<>(Collections.reverseOrder(new OrderComparator()));\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        for(int[] order: orders){\n            boolean isBuyOrder = order[2]==0?true:false;\n            if(isBuyOrder){\n                Order buyOrder = new BuyOrder();\n                buyOrder.price=order[0];\n                buyOrder.amount=order[1];\n                //execute order\n                while(sellOrders.peek()!=null && sellOrders.peek().price<=buyOrder.price){\n                    Order sellOrder = sellOrders.peek();\n                    if(buyOrder.amount>sellOrder.amount){\n                        sellOrders.poll();\n                        buyOrder.amount -=sellOrder.amount;\n                    } else if (buyOrder.amount < sellOrder.amount){\n                        sellOrder.amount-=buyOrder.amount;\n                        buyOrder.amount=0;\n                        break;\n                    } else {\n                        buyOrder.amount=0;\n                        sellOrders.poll();\n                        break;\n                    }\n                }\n                //push to backlog\n                if(buyOrder.amount>0){\n                    buyOrders.add(buyOrder);\n                }\n            }else {\n                Order sellOrder = new SellOrder();\n                sellOrder.price=order[0];\n                sellOrder.amount=order[1];\n                //execute order\n                while(buyOrders.peek()!=null && buyOrders.peek().price>=sellOrder.price){\n                    Order buyOrder = buyOrders.peek();\n                    if(sellOrder.amount>buyOrder.amount){\n                        buyOrders.poll();\n                        sellOrder.amount -=buyOrder.amount;\n                    } else if (sellOrder.amount < buyOrder.amount){\n                        buyOrder.amount-=sellOrder.amount;\n                        sellOrder.amount=0;\n                        break;\n                    } else {\n                        sellOrder.amount=0;\n                        buyOrders.poll();\n                        break;\n                    }\n                }\n                //push to backlog\n                if(sellOrder.amount>0){\n                    sellOrders.add(sellOrder);\n                }\n            }\n        }\n        int mod = 1000000007;\n        long totalOrders=0;\n        while(buyOrders.peek()!=null){\n            totalOrders += buyOrders.poll().amount;\n        }\n        while(sellOrders.peek()!=null){\n            totalOrders +=sellOrders.poll().amount;\n        }\n        return (int)(totalOrders % mod);\n    }\n}\n```
['Java']