Maximum 69 Number (LeetCode 1323): Solution and Explanation
Given a positive integer made only of the digits 6 and 9, change at most one digit to get the largest possible number.
Intuition
There are only two possible changes: a 9 becomes a 6, or a 6 becomes a 9. The first always makes the number smaller, so the only change worth making is 6 into 9.
Turning a 6 into a 9 adds 3 times that digit's place value. A 6 in the thousands place adds 3000, a 6 in the ones place adds only 3.
Any gain from a higher place beats every gain from the places below it, so the best move is to flip the leftmost 6. If there is no 6, the number is already as large as it can be.
Approach: Flip the leftmost 6
- Convert
numto a list of digit characters. - Scan from the left. At the first
'6', replace it with'9'and stop. - Join the digits and convert back to an integer.
Step-by-step example
num = 9669
- Digits are 9, 6, 6, 9.
- i = 0: digit 9 is already the largest digit, so keep it and move right.
- i = 1: digit 6 in the hundreds place. Turning it into 9 adds 300. Flip it and stop.
- Join the digits: 9969.
Code: Python, Java, C++
1class Solution:
2 def maximum69Number(self, num: int) -> int:
3 digits = list(str(num))
4 for i in range(len(digits)):
5 if digits[i] == '6':
6 digits[i] = '9'
7 break
8 return int(''.join(digits))1class Solution {
2 public int maximum69Number(int num) {
3 char[] digits = String.valueOf(num).toCharArray();
4 for (int i = 0; i < digits.length; i++) {
5 if (digits[i] == '6') {
6 digits[i] = '9';
7 break;
8 }
9 }
10 return Integer.parseInt(new String(digits));
11 }
12}1class Solution {
2public:
3 int maximum69Number(int num) {
4 string digits = to_string(num);
5 for (int i = 0; i < (int)digits.size(); i++) {
6 if (digits[i] == '6') {
7 digits[i] = '9';
8 break;
9 }
10 }
11 return stoi(digits);
12 }
13};Time and space complexity
O(d)
We look at each of the d digits at most once, and d is at most 5 here.
O(d)
The digit list holds d characters. A purely arithmetic version that finds the highest 6 with division and adds 3 x 10^k uses O(1) space.
Other approaches
Arithmetic, no strings (time O(d), space O(1))
Walk the digits with num % 10 and num / 10, remembering the place value of the highest 6 seen. Return num + 3 * that place value, or num if no 6 was seen.
Try every single change (time O(d^2), space O(d))
For each position, build the number with that digit flipped and keep the maximum, including the unchanged number. Correct but does d times the work for no benefit.
Common mistakes
- Flipping every 6. Only one change is allowed.
- Flipping the last 6 instead of the first one. The rightmost 6 sits in the lowest place and gives the smallest gain.
- Forgetting that no change is allowed. When num has no 6, return it unchanged.
- Using a string replace that replaces all occurrences. In Python, pass a count: num_str.replace('6', '9', 1).
Frequently asked questions
Why is changing a 9 into a 6 never useful?
It lowers that digit and leaves the others the same, so the number always gets smaller. Making no change at all is always better.
Why does the leftmost 6 always win?
Flipping a 6 in place 10^k adds 3 x 10^k. That is larger than 3 x (10^(k-1) + ... + 1), so no change further right can beat it.
Is there a one-line answer?
In Python, int(str(num).replace('6', '9', 1)) does exactly this: replace only the first 6. It is the same greedy idea written with a library call.