Interesting heap solution | Beats 100% Python | Explained
Just though of an interesting heap solution.\n\nMaintain a heap of length 2, containing the maximum values seen for each digit sum.\n\nReturn the heap with the 
Just though of an interesting heap solution.\n\nMaintain a heap of length 2, containing the maximum values seen for each digit sum.\n\nReturn the heap with the best sum\n\n# Code\n```\nfrom heapq import heappush, heappop\nclass SizedHeap:\n    def __init__(self, size: int):\n        self.heap = []\n        self.size = size\n    \n    def push(self, element: int):\n        if len(self.heap) < self.size:\n            heappush(self.heap, element)\n        elif element > self.heap[0]:\n            heappop(self.heap)\n            heappush(self.heap, element)\n            \n    def len(self) -> int:\n        return len(self.heap)\n            \n    def sum(self) -> int:\n        return sum(self.heap)\n\nclass Solution:\n    def maximumSum(self, nums: List[int]) -> int:\n        n = len(nums)\n        sum_of_digits_max = defaultdict(lambda: SizedHeap(2))\n        ans = -1\n        \n        for i, num in enumerate(nums):\n            tmp = num\n            s = 0\n            while num:\n                s += num % 10\n                num = num // 10\n            sum_of_digits_max[s].push(tmp)\n        \n        for sum_digit in sum_of_digits_max:\n            if sum_of_digits_max[sum_digit].len() < 2: continue\n            ans = max(ans, sum_of_digits_max[sum_digit].sum())\n        return ans\n```
[]