Maximum 69 Number - Complete Solution Guide
Maximum 69 Number is LeetCode problem 1323, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 la
Detailed Explanation
Imagine you're given a number like `9669`, but with a twist: it's guaranteed to be made up *only* of the digits `6` and `9`. Your task is to find the largest possible number you can construct by flipping *at most one* digit. A `6` becomes a `9`, and a `9` becomes a `6`. The 'at most one' part is crucial – you can choose to make no change if that's the best option, or change just one digit.
Solution Approach
The core idea here is a greedy one, driven by the principles of place value. To make an integer as large as possible, you want its leftmost (most significant) digits to be the biggest they can be. Given that our only options are 6s and 9s, and we can only change *one* digit, the most impactful change we can make to increase the number is to turn a `6` into a `9`. Crucially, this change should happen at the highest possible place value – meaning, the leftmost `6` you encounter. Changing a `9` to a `6` would always decrease the number, which is counterproductive to our goal of maximization. Therefore, the strategy simplifies to: find the very first (leftmost) `6` in the number and transform it into a `9`. If there are no `6`s, the number is already all `9`s, which is the maximum possible state, so no change is needed or beneficial. The provided solution elegantly implements this by converting the integer to a string, using Python's `str.replace('6', '9', 1)` to replace only the *first* occurrence of '6' with '9', and then converting it back to an integer.
Step-by-Step Algorithm
- Step 1: Convert the input integer `num` to a string `s`.
- Step 2: Iterate through the string `s` from left to right.
- Step 3: If a digit '6' is encountered, replace it with '9' and immediately break the loop.
- Step 4: Convert the modified string `s` back to an integer and return it. If no '6' was found, return the original `num`.
Key Insights
- **Maximization through Leftmost Impact:** To achieve the maximum number, changes should prioritize higher place values. Converting the leftmost '6' to a '9' ensures the largest possible increase, as it modifies the most significant digit that can be improved.
- **Unidirectional Change Focus:** The problem's objective to 'maximize' the number means we only care about transforming '6's into '9's. Flipping a '9' to a '6' would always decrease the number, thus it's an operation we actively avoid.
- **String Conversion for Direct Manipulation:** Representing the number as a string simplifies the task of identifying and modifying a specific digit based on its position. Python's `str.replace()` method, with its `count` argument, directly handles the 'at most one digit' constraint and targets the 'first' relevant digit efficiently.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Math, Greedy.
Companies
Asked at: Hudson River Trading.