Mid/SeniorGreedyMath

Maximum Swap

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

Return the maximum valued number you can get.

Example 1
Inputnum = 2736
Output7236
Swap the number 2 and the number 7.
Example 2
Inputnum = 9973
Output9973
No swap is needed because the number is already maximized.

Constraints

  • 0 <= num <= 10^8

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate