Search topics...
All Problems

Maximum Swap

Solution Approach
Was this helpful?
MediumarrayExpected: O(n) time, O(1) space

Problem

Given a non-negative integer, swap two digits at most once to get the maximum valued number.

Example 1:

Input: num = 2736
Output: 7236
Explanation: Swap digits 2 and 7.

Example 2:

Input: num = 9973
Output: 9973
Explanation: No swap needed — already the maximum.
Reference solution unlocks after your first submission
Loading...