<p>You are given an integer <code>num</code>. You can swap two digits at most once to get the maximum valued number.</p>

<p>Return <em>the maximum valued number you can get</em>.</p>

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

<pre>
<strong>Input:</strong> num = 2736
<strong>Output:</strong> 7236
<strong>Explanation:</strong> Swap the number 2 and the number 7.
</pre>

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

<pre>
<strong>Input:</strong> num = 9973
<strong>Output:</strong> 9973
<strong>Explanation:</strong> No swap.
</pre>

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

<ul>
	<li><code>0 &lt;= num &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution:
    def maximumSwap(self, num: int) -> int:
        """
        digits = []
        while num:
            digits.append(num % 10)
            num = num // 10
        k = len(digits)
        while k > 0 and max(digits[:k]) == digits[k-1]:
            k -= 1
        if k > 0:
            mi = k-1
            for i in range(0,k):
                if digits[i] > digits[mi]:
                    mi = i
            digits[k-1], digits[mi] = digits[mi], digits[k-1]
        val = 0
        k = 1
        for digit in digits:
            val += digit * k
            k = k*10
        return val
        """
        A = [int(x) for x in str(num)]
        last = {x: i for i, x in enumerate(A)}
        #print(last)
        for i, x in enumerate(A):
            #print(i, x)
            for d in range(9, int(x), -1):
                #print(x, d)
                if last.get(d, -1) > i:
                    A[i], A[last[d]] = A[last[d]], A[i]
                    #print(A)
                    return int("".join(map(str, A)))
        return num
16300