Given a non-negative integer num
, you are allowed to swap two digits at most once to get the maximum valued number possible. Return the maximum number you can get.
num
(not necessarily adjacent).num
itself.Constraints:
0 <= num <= 10^8
To maximize the number by swapping two digits, we want to bring the largest possible digit as far to the left as possible. The naive or brute-force approach would be to try all possible pairs of digits, swap them, and check which swap gives the largest number. However, this is inefficient, especially for numbers with many digits.
Instead, we can optimize by observing that the leftmost digit that can be increased by swapping with a larger digit on its right should be swapped. The key is to find the first position from the left where a larger digit exists somewhere to its right, and swap it with the largest such digit as far to the right as possible.
This approach avoids unnecessary swaps and ensures the maximal number is achieved in a single pass.
Let's break down the steps for an efficient solution:
We use a last-occurrence map because it lets us quickly find the best digit to swap with in O(1) time per digit.
Let's walk through the process with num = 2736
:
2736
to digits: [2, 7, 3, 6]
If no larger digit exists to the right of any digit, the number is already maximal.
The optimized approach is much more efficient and scalable, especially for larger numbers.
class Solution:
def maximumSwap(self, num: int) -> int:
digits = list(str(num))
last = {int(x): i for i, x in enumerate(digits)}
for i, x in enumerate(digits):
for d in range(9, int(x), -1):
if last.get(d, -1) > i:
digits[i], digits[last[d]] = digits[last[d]], digits[i]
return int(''.join(digits))
return num
class Solution {
public:
int maximumSwap(int num) {
string s = to_string(num);
int last[10] = {0};
for (int i = 0; i < s.size(); ++i) {
last[s[i] - '0'] = i;
}
for (int i = 0; i < s.size(); ++i) {
for (int d = 9; d > s[i] - '0'; --d) {
if (last[d] > i) {
swap(s[i], s[last[d]]);
return stoi(s);
}
}
}
return num;
}
};
class Solution {
public int maximumSwap(int num) {
char[] digits = Integer.toString(num).toCharArray();
int[] last = new int[10];
for (int i = 0; i < digits.length; i++) {
last[digits[i] - '0'] = i;
}
for (int i = 0; i < digits.length; i++) {
for (int d = 9; d > digits[i] - '0'; d--) {
if (last[d] > i) {
char temp = digits[i];
digits[i] = digits[last[d]];
digits[last[d]] = temp;
return Integer.parseInt(new String(digits));
}
}
}
return num;
}
}
var maximumSwap = function(num) {
let digits = num.toString().split('');
let last = new Array(10).fill(-1);
for (let i = 0; i < digits.length; i++) {
last[parseInt(digits[i])] = i;
}
for (let i = 0; i < digits.length; i++) {
for (let d = 9; d > parseInt(digits[i]); d--) {
if (last[d] > i) {
let temp = digits[i];
digits[i] = digits[last[d]];
digits[last[d]] = temp;
return parseInt(digits.join(''));
}
}
}
return num;
};
The Maximum Swap problem asks us to maximize a number by swapping two digits at most once. The key insight is to swap the leftmost digit with the largest possible digit to its right, using a last-occurrence lookup for efficiency. This yields an optimal solution in linear time, making the approach both elegant and practical for large numbers.