Python // 2 Heaps // Readable code with dataclass
The readability of your code is as important as the correctness of your solution.\n\nWhenever possible, I like to convert input arrays into custom objects with 
The readability of your code is as important as the correctness of your solution.\n\nWhenever possible, I like to convert input arrays into custom objects with readable field names.\n\n```Python\nfrom enum import IntEnum\nfrom dataclasses import dataclass, field\n\nclass oType(IntEnum):\n    BUY = 0\n    SELL = 1\n\n# Sort based on price. (Other fields not used in compare).\n@dataclass(order = True)\nclass Order:\n    price: int\n    amount: int = field(compare = False)\n    orderType: oType = field(compare = False)\n\n\nclass Solution:\n    def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:\n        orders = [Order(*o) for o in orders]\n        \n        buyHeap: Order = []\n        sellHeap: Order = []\n        \n        for incomingOrder in orders:\n            if incomingOrder.orderType == oType.BUY:\n                while sellHeap and sellHeap[0].price <= incomingOrder.price:\n                    sellOrder = heapq.heappop(sellHeap)\n                    if sellOrder.amount < incomingOrder.amount:\n                        incomingOrder.amount -= sellOrder.amount\n                    elif sellOrder.amount == incomingOrder.amount:\n                        incomingOrder.amount = 0\n                        break\n                    elif sellOrder.amount > incomingOrder.amount:\n                        sellOrder.amount = sellOrder.amount - incomingOrder.amount\n                        incomingOrder.amount = 0\n                        heapq.heappush(sellHeap, sellOrder)\n                        break\n                if incomingOrder.amount > 0:\n                    # negate for max heap\n                    incomingOrder.price = -incomingOrder.price\n                    heapq.heappush(buyHeap, incomingOrder)\n            elif incomingOrder.orderType == oType.SELL:\n                while buyHeap and -buyHeap[0].price >= incomingOrder.price:\n                    buyOrder = heapq.heappop(buyHeap)\n                    if buyOrder.amount < incomingOrder.amount:\n                        incomingOrder.amount -= buyOrder.amount\n                    elif buyOrder.amount == incomingOrder.amount:\n                        incomingOrder.amount = 0\n                        break\n                    elif buyOrder.amount > incomingOrder.amount:\n                        buyOrder.amount = buyOrder.amount - incomingOrder.amount\n                        incomingOrder.amount = 0\n                        heapq.heappush(buyHeap, buyOrder)\n                        break\n                if incomingOrder.amount > 0:\n                    heapq.heappush(sellHeap, incomingOrder)\n            \n        buySum = sum([order.amount for order in buyHeap])\n        sellSum = sum([order.amount for order in sellHeap])\n        return (buySum + sellSum) % (10**9 + 7)\n```\n                    \n                    \n                        \n
[]