Python - Priority Queue Solution - O(nlogn)
Maintain two priority queues. One for the buy orders and the other for the sell orders\n\nfrom queue import PriorityQueue\nclass Solution:\n    def getNumberOfB
Maintain two priority queues. One for the buy orders and the other for the sell orders\n```\nfrom queue import PriorityQueue\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        buy_q = PriorityQueue()\n        sell_q = PriorityQueue()\n\n        for odr in orders:\n            if odr[2] == 1:  #sell\n                while buy_q.qsize()>0 and odr[1] > 0: #while there is backlog and odr[1]!=0\n                    price, quant = buy_q.get()                      \n                    if -price < odr[0]:              #if the highest order price in buy_q is less than current order price\n                        buy_q.put((price,quant))\n                        break\n                        \n                    deal = min(quant, odr[1])    #max buy backlog that can be removed from buy\n                    odr[1] -= deal                #subract from the current sell quantity\n                    \n                    if deal != quant: buy_q.put((price, quant - deal))  #push the remaining buy backlog the the buy_q\n \n                if odr[1]>0:\n                    sell_q.put((odr[0], odr[1]))   #push the remaning sell items to the sell_q\n         \n            else:  #buy\n                while sell_q.qsize()>0 and odr[1] > 0: #while there is backlog and odr[1]!=0\n                    price, quant = sell_q.get()   \n                    if price > odr[0]:     #if the lowest order price in sell_q is less than current order price\n                        sell_q.put((price,quant))\n                        break\n                        \n                    deal = min(quant, odr[1]) #max sell backlog that can be removed from sell\n                    odr[1] -= deal   #subract from the current buy quantity\n                    \n                    if deal != quant: sell_q.put((price, quant - deal)) #push the remaining sell backlog the the sell_q\n                \n                if odr[1]>0:\n                    buy_q.put((-odr[0], odr[1])) #push the remaning buy items to the buy_q\n              \n        total_back = 0   #total backlog\n        while sell_q.qsize()>0:\n            total_back += sell_q.get()[1]\n\n        while buy_q.qsize() > 0:\n            total_back += buy_q.get()[1]\n        \n        return total_back%(10**9 + 7)\n```\n
[]