Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.
Example 1:
Input: 2736
Output: 7236
Explanation: Swap the number 2 and the number 7.
Example 2:
Input: 9973
Output: 9973
Explanation: No swap.
Note:
The given number is in the range [0, 108]
class Solution:
     def maximumSwap(self, num):
         """
         :type num: int
         :rtype: int
         """
         import itertools
         
         # Convert num into a list of chars
         A = [c for c in str(num)]
         
         # Compute suffix max for A
         suff_max = list(itertools.accumulate(A[::-1], max))[::-1]
         
         # Compute char -> largest index in A
         last = {x : i for i, x in enumerate(A)}
 
         # Swap the first occurrence of A[i] < suff_max[i]
         for i, x in enumerate(A):
             if x < suff_max[i]:
                 A[i], A[last[suff_max[i]]] = A[last[suff_max[i]]], A[i]
                 break
 
         return int(''.join(A))
class Solution:
     def maximumSwap(self, num):
         """
         :type num: int
         :rtype: int
         """
         import itertools
         
         # Convert num into a list of chars
         A = [c for c in str(num)]
         
         # Compute suffix max for A
         suff_max = list(itertools.accumulate(A[::-1], max))[::-1]
         
         # Compute char -> largest index in A
         last = {x : i for i, x in enumerate(A)}
 
         # Swap the first occurrence of A[i] < suff_max[i]
         for i, x in enumerate(A):
             if x < suff_max[i]:
                 A[i], A[last[suff_max[i]]] = A[last[suff_max[i]]], A[i]
                 break
 
         return int(''.join(A))