<p>You are given an integer array <code>nums</code>. You have to find the <strong>maximum</strong> sum of a pair of numbers from <code>nums</code> such that the <strong>largest digit </strong>in both numbers is equal.</p>

<p>For example, 2373 is made up of three distinct digits: 2, 3, and 7, where 7 is the largest among them.</p>

<p>Return the <strong>maximum</strong> sum or -1 if no such pair exists.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [112,131,411]</span></p>

<p><strong>Output:</strong> <span class="example-io">-1</span></p>

<p><strong>Explanation:</strong></p>

<p>Each numbers largest digit in order is [2,3,4].</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2536,1613,3366,162]</span></p>

<p><strong>Output:</strong> <span class="example-io">5902</span></p>

<p><strong>Explanation:</strong></p>

<p>All the numbers have 6 as their largest digit, so the answer is <span class="example-io">2536 + 3366 = 5902.</span></p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [51,71,17,24,42]</span></p>

<p><strong>Output:</strong> <span class="example-io">88</span></p>

<p><strong>Explanation:</strong></p>

<p>Each number&#39;s largest digit in order is [5,7,7,4,4].</p>

<p>So we have only two possible pairs, 71 + 17 = 88 and 24 + 42 = 66.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 100</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:

    def maxSum(self, nums: List[int]) -> int:
        # nums = [51,71,17,24,42]  #> 71+17=88
        # nums = [31,25,72,79,74]  # 72+74=146, not 79,74
        # nums = [84,91,18,59,27,9,81,33,17,58]     # 84+81=165, not 91,59
        
        max_digits = [max(int(digit) for digit in str(num)) for num in nums]
        max_digits_uniq = list(set(max_digits))
        max_digits_uniq.sort(reverse=True)

        max_digit_pairs = [] 
        for digit in max_digits_uniq:
            if max_digits.count(digit) >= 2:
                # found the max digit that have a least pair of nums
                max_digit_pairs.append(digit)

        if len(max_digit_pairs) < 1:
            return -1

        # found largest digit pairs 
        max_nums = []

        for digit in max_digit_pairs:
            pair = []
            min_nums = [-num for num in nums]
            heapq.heapify(min_nums)
            
            while len(pair) < 2:
                num = heapq.heappop(min_nums)
                num_max_digit = max([int(c) for c in str(-num)])
                if str(digit) in str(num) and digit == num_max_digit:
                    pair.append(-num)
            
            max_nums.append(sum(pair))

        max_nums.sort(reverse=True)

        return max_nums[0]       
200