You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.
Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.
Notes:
d1 in num with d2.num does not change.Example 1:
Input: num = 11891 Output: 99009 Explanation: To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899. To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890. The difference between these two numbers is 99009.
Example 2:
Input: num = 90 Output: 99 Explanation: The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0). Thus, we return 99.
Constraints:
1 <= num <= 108When 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:
We want to find the biggest and smallest numbers we can make by replacing one digit in the original number with another. The brute force way is to try *every* possible replacement and see what happens.
Here's how the algorithm would work step-by-step:
def max_difference_by_remapping_a_digit(number):
number_string = str(number)
remapped_numbers = []
# Iterate through all possible replacement digits
for replacement_digit in range(10):
# Iterate through all possible digits to be replaced
for digit_to_replace in range(10):
new_number_string = ""
# Create the remapped number
for digit_char in number_string:
if int(digit_char) == digit_to_replace:
new_number_string += str(replacement_digit)
else:
new_number_string += digit_char
# Prevent leading zeros and append to the list
if len(new_number_string) > 1 and new_number_string[0] == '0':
continue
remapped_numbers.append(int(new_number_string))
# Handle the case when no valid remapped numbers exist
if not remapped_numbers:
return 0
# Find max and min remapped numbers
max_number = max(remapped_numbers)
min_number = min(remapped_numbers)
# The maximum difference is the difference between max and min
maximum_difference = max_number - min_number
return maximum_differenceTo find the maximum difference, we aim to create the largest and smallest possible numbers by remapping a single digit. The key is to choose the digit and the remapping value intelligently for both maximum and minimum values.
Here's how the algorithm would work step-by-step:
def max_difference_by_remapping_digit(number):
number_string = str(number)
# Construct the largest possible number
largest_number_string = list(number_string)
digit_to_replace_max = None
for digit in largest_number_string:
if digit != '9':
digit_to_replace_max = digit
break
if digit_to_replace_max is not None:
for i in range(len(largest_number_string)):
if largest_number_string[i] == digit_to_replace_max:
largest_number_string[i] = '9'
largest_number = int("".join(largest_number_string))
else:
largest_number = number
# Construct the smallest possible number
smallest_number_string = list(number_string)
digit_to_replace_min = None
# Need to handle the first digit carefully
if smallest_number_string[0] != '1':
digit_to_replace_min = smallest_number_string[0]
replacement_digit = '1'
else:
replacement_digit = '0'
for i in range(1, len(smallest_number_string)):
if smallest_number_string[i] != '0' and smallest_number_string[i] != '1':
digit_to_replace_min = smallest_number_string[i]
break
if digit_to_replace_min is not None:
for i in range(len(smallest_number_string)):
if smallest_number_string[i] == digit_to_replace_min:
smallest_number_string[i] = replacement_digit
smallest_number = int("".join(smallest_number_string))
else:
smallest_number = number
# Calculate the maximum difference
max_difference = largest_number - smallest_number
return max_difference| Case | How to Handle |
|---|---|
| Input number is 0 | Remapping to 9 gives the maximum possible number, and remapping to 0 gives the minimum possible number. |
| Input number consists of all the same digits (e.g., 11111) | Remapping all digits to 9 for max and all digits to 0 (if not leading digit) or 1 (if leading digit) for min must be handled carefully. |
| Large input number that could cause integer overflow when remapping | Use long type for intermediate calculations or string manipulation to avoid overflow. |
| Leading digit is remapped to 0 for minimum, resulting in invalid number representation | If the leading digit is remapped to 0, find the smallest non-zero digit and remap that to 0 instead. |
| The largest digit is already 9, so no remapping can increase its value | In such cases, find the next largest and remap that. |
| The smallest digit is already 0, so no remapping can decrease its value | In such cases, find the next smallest and remap that. |
| Input number is a single digit number | Remap the digit to 9 for max and to 0 for min. |
| No digit replacement results in a change to the input number | For example the largest possible is already the number, so the subtraction will result in 0. |