670. Maximum Swap
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 52% Topics: Math, Greedy
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(logn), logn is the length of the number string
# Space: O(logn)
class Solution(object):
def maximumSwap(self, num):
"""
:type num: int
:rtype: int
"""
digits = list(str(num))
left, right = 0, 0
max_idx = len(digits)-1
for i in reversed(xrange(len(digits))):
if digits[i] > digits[max_idx]:
max_idx = i
elif digits[max_idx] > digits[i]:
left, right = i, max_idx
digits[left], digits[right] = digits[right], digits[left]
return int("".join(digits))
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions