Python3. Heap
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n\n        buy = []\n        sell = []\n\n        for order in orders:\n            price, amount, tp = order\n\n            if tp == 0:\n                while sell and sell[0][0] <= price:\n                    tmp = heapq.heappop(sell)\n                    if amount < tmp[1]:\n                        heapq.heappush(sell, (tmp[0], tmp[1] - amount))\n                        amount = 0\n                        break\n                    else:\n                        amount = amount - tmp[1]\n                \n                if amount != 0:\n                    heapq.heappush(buy, (-price, amount))\n\n            if tp == 1:\n                while buy and -buy[0][0] >= price:\n                    tmp = heapq.heappop(buy)\n                    if amount < tmp[1]:\n                        heapq.heappush(buy, (tmp[0], tmp[1] - amount))\n                        amount = 0\n                        break\n                    else:\n                        amount = amount - tmp[1]\n                \n                if amount != 0:\n                    heapq.heappush(sell, (price, amount))\n\n        cnt = 0\n\n        while sell:\n            tmp = heapq.heappop(sell)\n            cnt += tmp[1]\n\n        while buy:\n            tmp = heapq.heappop(buy)\n            cnt += tmp[1]\n\n        return cnt % (10 ** 9 + 7)\n```
['Python3']