two heaps
\n\n# Code\n\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        bidbook = []\n        askbook = []\n        for 
\n\n# Code\n```\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        bidbook = []\n        askbook = []\n        for price,amount,otype in orders:\n            if otype == 0:\n                while amount and askbook and askbook[0][0]<=price:\n                    askp,asks = heapq.heappop(askbook)\n                    if amount >= asks:\n                        amount -= asks\n                    else:\n                        asks -= amount\n                        amount = 0\n                        heapq.heappush(askbook,(askp,asks))\n                if amount:\n                    heapq.heappush(bidbook,(-price,amount))\n            else:\n                while amount and bidbook and -bidbook[0][0]>=price:\n                    bidp,bids = heapq.heappop(bidbook)\n                    if amount >= bids:\n                        amount -= bids\n                    else:\n                        bids -= amount\n                        amount = 0\n                        heapq.heappush(bidbook,(bidp,bids))\n                if amount:\n                    heapq.heappush(askbook,(price,amount))\n        res = 0\n        for p,m in bidbook:\n            res += m\n        for p,m in askbook:\n            res += m\n        return res % (10**9+7)\n                    \n\n\n        \n\n```
['Heap (Priority Queue)', 'Python3']