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 <= 108This approach involves iterating through each digit of the number. You need to determine which single digit change would lead to the greatest possible increase in the number (for the max value) and which single digit change would lead to the greatest possible decrease (for the min value). To find the max value, you should try remapping the first non-nine digit to nine. For the min value, try remapping the first non-zero digit to zero, keeping in mind about not creating a leading zero. Calculate the max and min numbers and derive the result by computing the difference between them.
This Python solution first converts the number to a string. We iterate through each digit to find a suitable candidate for remapping to achieve the maximum number by making the smallest digit to '9'. Similarly, another loop replaces the smallest digit with '0' while checking for the leading zero case for the minimum number.
C
Java
C#
JavaScript
Time Complexity: O(n), where n is the number of digits in the number. Space Complexity: O(n), due to string operations.
In this optimized approach, we try to minimize the iterations by directly determining the first target digit which is to be changed in order to generate either the largest possible number or the smallest possible number in one single sweep through the digit array. This process minimizes redundant checks and iterations by working based on the first impactful change alone.
This second Python solution optimizes by immediately remapping the first encounter to maximize values strategically and minimizes redundant calculations. It directly yields the most impactful change.
C
Java
C#
JavaScript
Time Complexity: O(n), where n is the number of digits. Space Complexity: O(n), due to string operations.
| Approach | Complexity |
|---|---|
| Digit-by-Digit Remapping Approach | Time Complexity: O(n), where n is the number of digits in the number. Space Complexity: O(n), due to string operations. |
| Single Pass Optimized Remapping Approach | Time Complexity: O(n), where n is the number of digits. Space Complexity: O(n), due to string operations. |
How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer • CrioDo • 304,679 views views
Watch 9 more video solutions →Practice Maximum Difference by Remapping a Digit with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor