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.The problem asks to find the maximum number possible by changing at most one digit in a given number (consisting only of digits 6 and 9) where a 6 can become a 9 and vice-versa. The most efficient approach is a greedy one.
Approach:
The optimal strategy is to find the first occurrence of the digit 6 from the left (most significant digit) and replace it with 9. Replacing a 6 with a 9 results in the largest possible increase in the number's value. If no 6 exists, the original number is already maximal, and no change is needed.
Algorithm:
Convert to String: Convert the input integer num
into a string. This allows easy access to individual digits.
Iterate and Replace: Iterate through the string representation of the number. If a '6' is found, replace it with a '9' and immediately break the loop (since we're allowed to change only one digit).
Convert Back to Integer: Convert the modified string back into an integer and return it.
Time Complexity Analysis:
Therefore, the overall time complexity is O(log₁₀(num)), which is linear in the number of digits. This is very efficient.
Space Complexity Analysis:
The space complexity is O(log₁₀(num)) because the space used is proportional to the length of the string representation of the number. This is also linear in the number of digits.
Code Examples (with detailed comments):
Python:
class Solution:
def maximum69Number(self, num: int) -> int:
s_num = str(num) # Convert to string for easy digit access
modified_num = "" # Initialize the modified string
found_six = False # Flag to track if a '6' has been replaced
for digit in s_num:
if digit == '6' and not found_six:
modified_num += '9' #Replace the first '6' with '9'
found_six = True
else:
modified_num += digit #Append rest of digits as they are
return int(modified_num) # Convert back to an integer
Java:
class Solution {
public int maximum69Number(int num) {
String sNum = Integer.toString(num); // Convert to String
StringBuilder sb = new StringBuilder(); //More efficient than String concatenation
boolean sixFound = false;
for (char c : sNum.toCharArray()) {
if (c == '6' && !sixFound) {
sb.append('9');
sixFound = true;
} else {
sb.append(c);
}
}
return Integer.parseInt(sb.toString()); // Convert back to integer
}
}
The other languages (C++, Go, TypeScript, Rust, PHP, C) follow similar logic, primarily focusing on converting the number to a string, modifying the string by replacing the first '6' with '9', and then converting it back to an integer. The choice of using string manipulation is the most readable and concise way to solve this problem. The C example demonstrates a slightly different approach that avoids string manipulation using mathematical operations. It's less readable but equally efficient.