You are given a positive integer num
consisting only of digits 6
and 9
.
Return the maximum number you can get by changing at most one digit (6
becomes 9
, and 9
becomes 6
).
Example 1:
Input: num = 9669 Output: 9969 Explanation: Changing the first digit results in 6669. Changing the second digit results in 9969. Changing the third digit results in 9699. Changing the fourth digit results in 9666. The maximum number is 9969.
Example 2:
Input: num = 9996 Output: 9999 Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:
Input: num = 9999 Output: 9999 Explanation: It is better not to apply any change.
Constraints:
1 <= num <= 104
num
consists of only 6
and 9
digits.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 involves checking every possible change we can make to the number. We'll try flipping each 6 to a 9, one at a time, and keep track of which flip gives us the largest possible number.
Here's how the algorithm would work step-by-step:
def maximum_69_number_brute_force(number):
number_string = str(number)
maximum_number = number
# Iterate to find each '6' to potentially replace
for index in range(len(number_string)):
if number_string[index] == '6':
# Create a new number string with the flipped digit
modified_number_string = list(number_string)
modified_number_string[index] = '9'
modified_number = int("".join(modified_number_string))
# Check if this flip created a larger number
if modified_number > maximum_number:
maximum_number = modified_number
return maximum_number
The goal is to find the largest possible number by changing at most one digit in the given number. The clever trick is to only consider changing the leftmost '6' to a '9', because doing so will always yield the largest possible increase in the number's value.
Here's how the algorithm would work step-by-step:
def maximum69Number(number):
number_string = str(number)
# Iterate through the digits to find the leftmost '6'
for index, digit in enumerate(number_string):
if digit == '6':
# Change the first '6' to '9'
modified_number_string = number_string[:index] + '9' + number_string[index+1:]
# Return the modified number as an integer
return int(modified_number_string)
# If no '6' is found, return the original number
return number
Case | How to Handle |
---|---|
Input is a single digit 6 | The solution should change it to 9 and return 9. |
Input is a single digit 9 | The solution should return 9 as no change results in a larger number. |
Input consists of all 9s | The solution should return the original number as no change makes it larger. |
Input consists of all 6s | The solution should change the first 6 to a 9 to maximize the number. |
Input starts with 6 followed by only 9s | Changing the first 6 to 9 will provide the largest possible outcome. |
Integer Overflow after changing a digit | This is not applicable since the number of digits is relatively small and conversion back to integer will not lead to overflow. |
Input contains leading zeros (though not explicitly disallowed by prompt) | Treat the input as a number and leading zeros are implicitly dropped; the algorithm proceeds as normal. |
Maximum integer value consisting of only 6s and 9s (close to Integer.MAX_VALUE) | The algorithm modifies at most one digit, so it won't exceed the maximum integer value representable. |