[Python3] priority queue
\n\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        ans = 0\n        buy, sell = [], [] # max-heap & min-heap 
\n```\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        ans = 0\n        buy, sell = [], [] # max-heap & min-heap \n        \n        for p, q, t in orders: \n            ans += q\n            if t: # sell order\n                while q and buy and -buy[0][0] >= p: # match \n                    pb, qb = heappop(buy)\n                    ans -= 2*min(q, qb)\n                    if q < qb: \n                        heappush(buy, (pb, qb-q))\n                        q = 0 \n                    else: q -= qb \n                if q: heappush(sell, (p, q))\n            else: # buy order \n                while q and sell and sell[0][0] <= p: # match \n                    ps, qs = heappop(sell)\n                    ans -= 2*min(q, qs)\n                    if q < qs: \n                        heappush(sell, (ps, qs-q))\n                        q = 0 \n                    else: q -= qs \n                if q: heappush(buy, (-p, q))\n            \n        return ans % 1_000_000_007\n```\n\nA conciser implementation by @lee215\n```\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        buy, sell = [], [] # max-heap & min-heap \n        for p, q, t in orders: \n            if t: heappush(sell, [p, q])\n            else: heappush(buy, [-p, q])\n            \n            while buy and sell and -buy[0][0] >= sell[0][0]: \n                qty = min(buy[0][1], sell[0][1])\n                buy[0][1] -= qty\n                sell[0][1] -= qty\n                if not buy[0][1]: heappop(buy)\n                if not sell[0][1]: heappop(sell)\n        return (sum(q for _, q in sell) + sum(q for _, q in buy)) % 1_000_000_007\n```
['Python3']