Production Ready Solution: Min Heap - Max Heap Approach (Python)
Approach\nUse Max Heap to store buy orders\nUse Min Heap to store sell orders\n\neach heap item should store a tuple of (price, amount)\n\n# Complexity\n- Time 
# Approach\nUse Max Heap to store buy orders\nUse Min Heap to store sell orders\n\neach heap item should store a tuple of (price, amount)\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def __init__(self):\n        self.buy_heap = []  # Max-heap (inverted min-heap)\n        self.sell_heap = []  # Min-heap\n        self.mod = 10**9 + 7\n\n    def handleBuyOrder(self, price, amount):\n        """Process buy orders by matching with the lowest sell order available.\n\n        Args:\n            price (int): Price of the buy order.\n            amount (int): Amount of the buy order.\n        """\n        while amount > 0 and self.sell_heap:\n            min_sell_price, min_sell_amount = self.sell_heap[0]\n            if min_sell_price <= price:\n                if min_sell_amount <= amount:\n                    amount -= min_sell_amount\n                    heapq.heappop(self.sell_heap)\n                else:\n                    self.sell_heap[0] = (min_sell_price, min_sell_amount - amount)\n                    amount = 0\n            else:\n                break\n\n        if amount > 0:\n            heapq.heappush(self.buy_heap, (-price, amount))\n\n    def handleSellOrder(self, price, amount):\n        """Process sell orders by matching with the highest buy order available.\n\n        Args:\n            price (int): Price of the sell order.\n            amount (int): Amount of the sell order.\n        """\n        while amount > 0 and self.buy_heap:\n            max_buy_price, max_buy_amount = -self.buy_heap[0][0], self.buy_heap[0][1]\n            if max_buy_price >= price:\n                if max_buy_amount <= amount:\n                    amount -= max_buy_amount\n                    heapq.heappop(self.buy_heap)\n                else:\n                    self.buy_heap[0] = (-max_buy_price, max_buy_amount - amount)\n                    amount = 0\n            else:\n                break\n\n        if amount > 0:\n            heapq.heappush(self.sell_heap, (price, amount))\n\n    def getNumberOfBacklogOrders(self, orders):\n        """Calculate the total number of backlog orders after processing all incoming orders.\n\n        Args:\n            orders (List[List[int]]): List of orders, each represented as [price, amount, orderType]\n\n        Returns:\n            int: Total number of orders remaining in the backlog.\n        """\n        for price, amount, orderType in orders:\n            if orderType == 0:\n                self.handleBuyOrder(price, amount)\n            elif orderType == 1:\n                self.handleSellOrder(price, amount)\n\n        backlog_count = (sum(amount for _, amount in self.buy_heap) +\n                         sum(amount for _, amount in self.sell_heap))\n        return backlog_count % self.mod\n```
['Python']