Maximum 69 Number - Problem
Imagine you have a special number made up of only 6s and 9s - like a digital display where some segments might be flipped! Your mission is to make this number as large as possible by flipping at most one digit.
๐ The Rule: You can change exactly one digit:
6 โ 9(increases the number)9 โ 6(decreases the number)
Goal: Return the maximum possible number you can achieve.
Example: Given 9669, you can change the first 6 to 9 to get 9969 - but is this optimal? ๐ค
Input & Output
example_1.py โ Basic Case
$
Input:
num = 9669
โบ
Output:
9969
๐ก Note:
We can change the first 6 to 9 to get 9969, which is the maximum possible.
example_2.py โ All 9s
$
Input:
num = 9996
โบ
Output:
9969
๐ก Note:
No 6s to change to 9, so we change the rightmost 9 to 6 to minimize the decrease.
example_3.py โ Single Digit
$
Input:
num = 9
โบ
Output:
6
๐ก Note:
Only one digit available, and since it's 9, we change it to 6.
Constraints
- 1 โค num โค 104
- num consists only of digits 6 and 9
- You can change at most one digit
Visualization
Tap to expand
Understanding the Visualization
1
Scan from Left
Start from the most significant digit - changes here have the biggest impact
2
Find First 6
The first 6 we encounter can be flipped to 9 for maximum gain
3
Flip and Return
Change that 6 to 9 and we're done - this gives us the maximum possible value
4
All 9s Case
If no 6s exist, flip the rightmost 9 to 6 to minimize the loss
Key Takeaway
๐ฏ Key Insight: The greedy approach works because changing the leftmost digit has the maximum impact on the final number value. Always prioritize the most significant positions!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code