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:
Input: num = 2736 Output: 7236 Explanation: Swap the number 2 and the number 7.
Example 2:
Input: num = 9973 Output: 9973 Explanation: No swap.
Constraints:
0 <= num <= 108
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the maximum swap involves trying every possible pair of digits to swap. We explore all swap combinations and see which swap produces the largest possible number.
Here's how the algorithm would work step-by-step:
def maximum_swap_brute_force(number):
number_string = str(number)
maximum_number = number
# Iterate through all possible pairs of digits
for first_index in range(len(number_string)):
for second_index in range(first_index + 1, len(number_string)):
# Convert the string to a list to allow swapping
number_list = list(number_string)
# Perform the swap
number_list[first_index], number_list[second_index] = number_list[second_index], number_list[first_index]
# Convert the list back to a string and then to an integer
swapped_number = int("".join(number_list))
# Check if this swap yields a larger number
if swapped_number > maximum_number:
maximum_number = swapped_number
return maximum_number
The most efficient way to solve this is to find the biggest digit to the right of each digit, and then swap. This avoids unnecessary checks and leads directly to the largest possible result. We prioritize finding the best possible swap early to maximize the number's value.
Here's how the algorithm would work step-by-step:
def maximum_swap(number):
number_string = list(str(number))
length_of_number = len(number_string)
index_of_largest = -1
index_to_swap_with = -1
for current_index in range(length_of_number):
largest_digit_index = current_index
for right_index in range(current_index + 1, length_of_number):
# Find the largest digit on the right
if number_string[right_index] >= number_string[largest_digit_index]:
largest_digit_index = right_index
# Need to swap if a larger digit exists on the right.
if number_string[largest_digit_index] > number_string[current_index]:
index_of_largest = largest_digit_index
index_to_swap_with = current_index
break
#If a swap can be made, then swap
if index_of_largest != -1:
number_string[index_of_largest], number_string[index_to_swap_with] = number_string[index_to_swap_with], number_string[index_of_largest]
return int("".join(number_string))
Case | How to Handle |
---|---|
Null or empty input string | Return the original string if the input is null or empty to avoid null pointer exceptions and handle trivial cases. |
String of length 1 | Return the original string as no swap is possible with a single character. |
String with all identical characters (e.g., '1111') | Return the original string since no swap can increase the value. |
Maximum-sized integer as a string (e.g., '9999999999') | Ensure the algorithm doesn't overflow when converting substrings to integers during the swap process (although we can assume it is not necessary for this question if we are swapping characters within the string). |
String with leading zeros (e.g., '00123') | The algorithm should correctly handle leading zeros, comparing '0' with other digits without causing errors. |
No possible swap leads to a larger number | The algorithm should iterate through all possible swaps and return the original string if no swap produces a larger number. |
String representing a negative number | The algorithm should treat the minus sign as any other character (or, if we are told it is guaranteed to be a positive number, we don't handle it specifically). |
Integer overflow if converting the string to an integer for comparison. | The algorithm avoids integer overflow by working directly on the string representation and comparing substrings instead of converting the entire string to an integer. |