Python solution using minheap and maxheap
\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        \'\'\'\n        2D array orders \n        orders[i] = [price
```\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        \'\'\'\n        2D array orders \n        orders[i] = [price, amount, orderType]\n        0 = buy\n        1 = sell\n        \n        backlog = []\n        \n        if order == buy:\n            if any get the sell_order with smallest price. \n                - sell removed from the backlog\n            else:\n                - buy is added to the backlog\n        if sell:\n            if any get the buy_order with largest price,\n                - buy order is removed from the backlog\n            else:\n                - sell is added to the backlog\n            \n        \'\'\'\n        minheap = []\n        maxheap = []\n        \n        BUY, SELL = 0, 1\n        \n        for price, order, typ in orders:\n            consume = 0\n            if typ is BUY:\n                while minheap and order and minheap[0][0] <= price:\n                    sellPrice, sellOrder = heappop(minheap)\n                    consume = min(order, sellOrder)\n                    sellOrder -= consume\n                    order -= consume\n                    if sellOrder:\n                        heappush(minheap, (sellPrice, sellOrder))\n                 \n                if order: #if any buy orders are remaining\n                    heappush(maxheap, (-price, order))\n                    \n            else: #order is SELL\n                while maxheap and order and -maxheap[0][0] >= price:\n                    buyPrice, buyOrder = heappop(maxheap)\n                    consume = min(order, buyOrder)\n                    buyOrder -= consume\n                    order -= consume\n                    if buyOrder:\n                        heappush(maxheap, (buyPrice, buyOrder))\n                 \n                if order: #if any sell orders are remaining\n                    heappush(minheap, (price, order))\n        \n        totorder = 0\n        for _,order in minheap:\n            totorder += order\n            \n        for _,order in maxheap:\n            totorder += order\n            \n        return totorder % (10**9 + 7)\n            \n```
['Heap (Priority Queue)', 'Python']