Python 3, Heap and Dictionary
We use two heaps:\nlogBuy is storing MINUS prices, because we will need the highest price first.\nlogSell is storing prices, because we will need the lowest pri
We use two heaps:\n```logBuy``` is storing MINUS prices, because we will need the highest price first.\n```logSell``` is storing prices, because we will need the lowest price first.\n\nWe use two dictonaries:\n```dictBuy[k]``` is number of prices ```-k``` in ```Buy Backlog```\n```dictSell[k]``` is number of prices ```k``` in ```Sell Backlog```\n\nFirst we are iterating over ```orders``` and updating heaps and dictionaries.\nFinally we are counting, how many items is in Backlogs and return that value.\n\n```\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        logBuy=[]\n        dictBuy={}\n        logSell=[]\n        dictSell={}\n        #   price,amount,type\n        for p,a,t in orders:\n            if t: # type=1 = SELL\n                dictSell[p]=dictSell.get(p,0) + a\n                heapq.heappush(logSell,p) \n            else: # type=0 = BUY\n                dictBuy[-p]=dictBuy.get(-p,0) + a\n                heapq.heappush(logBuy,-p)\n            ok=1\n            while ok and logBuy and logSell:\n                ok=0\n                buy=heapq.heappop(logBuy)\n                sell=heapq.heappop(logSell)\n                b=dictBuy[buy]\n                s=dictSell[sell]\n                if b and s and -buy>=sell:\n                    ok=1\n                    amount=min(b,s)\n                    dictBuy[buy]-=amount\n                    dictSell[sell]-=amount\n                if dictBuy[buy]:\n                    heapq.heappush(logBuy,buy)\n                if dictSell[sell]:\n                    heapq.heappush(logSell,sell)                        \n        return (sum(v for k,v in dictBuy.items()) + sum(v for k,v in dictSell.items())) % (10**9+7)\n```
['Heap (Priority Queue)', 'Python']