class Solution:
def maxSum(self, nums: list[int]) -> int:
ht: dict[int, list[int]] = {}
result = -1
for num in nums:
tmp, max_digit = num, 0
while tmp:
tmp, max_digit = tmp // 10, max(max_digit, tmp % 10)
ht[max_digit] = sorted(ht.get(max_digit, []) + [num])[-2:]
if len(ht[max_digit]) == 2:
result = max(result, sum(ht[max_digit]))
return result
2815. Max Pair Sum in an Array - LeetCode
ยท One min read