Search⌘ K
AI Features

Solution: Maximum Swap

Explore how to maximize a number by swapping at most two digits once through a greedy algorithm. Learn to identify the optimal digits to swap by scanning from right to left, enabling efficient and optimal results in linear time. This lesson helps you implement the solution in JavaScript and understand its time and space complexity.

Statement

Given an integer num, return the maximum number that can be formed by swapping at most two digits once.

Constraints:

  • 00 \leq num 105\leq 10^5 ...