Record the last position of each digit, then scan left to right for the first improvable position.
Here's the solution:
function maximumSwap(num):
digits = list of digits of num
last = array of size 10
for i in range(len(digits)):
last[digits[i]] = i
for i in range(len(digits)):
for d in range(9, digits[i], -1):
if last[d] > i:
swap digits[i] and digits[last[d]]
return digits as number
return num
time, space where is the number of digits (at most ).