Leetcode 1323. Maximum 69 Number

Problem Explanation:

This problem presents a number made up only of the digits 6 and 9. It asks for the maximum possible number that can be made by changing at most one digit i.e converting a 6 to 9. In order to get the highest possible number, we have to replace the most significant 6 with a 9. This problem uses the principle of number systems where the digit on the left contributes more to the total value of the number than the digits on the right.

For instance, if we're given the number 9669, changing the first digit from 9 to 6 would result in 6669, which would reduce the total value of the number. The best way to get the maximum value is by changing the first 6 from the left to 9, which gives us 9969.

Problem Solution Approach:

The solution to this problem is straightforward. We first convert the number to a string, and then iterate over the string from left to right until we find the first 6 and change it to a 9. After that, we'll convert the string back into a number and return it as our answer.

We'll use the language's in-built mechanisms to iterate through the string, convert characters and numbers, and various string manipulation techniques.

Let's see how this can be implemented in Python, Java, JavaScript, C++, and C#.

Python Solution:

3class Solution(object):
4    def maximum69Number(self, num):
5        """
6        :type num: int
7        :rtype: int
8        """
9        # Convert the number to a string
10        str_num = str(num)
11        # Replace the first occurrence of '6' with '9'
12        str_num = str_num.replace('6', '9', 1)
13        # Convert the string back to integer and return
14        return int(str_num)

Java Solution:

3public class Solution {
4 public int maximum69Number (int num) {
5  String strNum = Integer.toString(num);
6  strNum = strNum.replaceFirst("6", "9");
7  return Integer.parseInt(strNum);
8 }

JavaScript Solution:

3class Solution {
4  maximum69Number(num) {
5    let strNum = num.toString();
6    strNum = strNum.replace('6', '9');
7    return parseInt(strNum);
8  }

C++ Solution:

3class Solution {
5    int maximum69Number (int num) {
6       std::string strNum = std::to_string(num);
7       std::replace(strNum.begin(), strNum.end(), '6', '9');
8       return stoi(strNum);
9    }

C# Solution:

3public class Solution {
4    public int Maximum69Number(int num) {
5      string strNum = num.ToString();
6      strNum = strNum.Replace('6', '9', 1);
7      return int.Parse(strNum);
8    }

With this approach, we can cater for all the digits in any given number. The trick is to replace the most significant 6 with 9 in order to gain the maximum possible number.With these solutions in mind, it is crucial to note a few things about our approach:

Firstly, we are able to avoid a slower brute force method in which we might iteratively change every 6 to a 9 to see which resulted in the highest number. This would be significantly slower and require more processing power, especially with larger numbers. Moreover, the solution presented in this article does not require any external libraries or tools beyond those already included in the corresponding languages.

Secondly, it is important to remember that these solutions handle situations where the number does not contain any digit '6'. If the number only contains 9's, these solutions will simply return the original number.

Additionally, it should be stressed that these solutions rely on the inbuilt capabilities of each language to convert numbers to strings and vice versa. Understanding how your specific language handles type conversion can be critical to solving problems like this.

Lastly, the underlying logic behind all these solutions stays the same, despite the syntax and language-specific methods. Regardless of whether you're using Python, Java, JavaScript, C++ or C#, you're converting the number to a string, replacing the first '6' with a '9', then converting it back to a number.

In conclusion, this problem helped us understand about the importance of positional values in our number system and how to make use of String manipulation functions to solve problems. This solution has an O(n) time complexity as we are looping through all characters of the string. Where n is the number of digits in the number. We are only making a constant amount of new space so the space complexity is O(1).

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫