1323. Maximum 69 Number

Maximum 69 Number

Maximum 69 Number


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.

Approach) Using Build Methods

  • First line: sets variable to number (num) converted to a string and then replaces 6 with 9. '.replace' will find the first instance of 6 and replace it with 9 and then end.
  • Then return the parseInt(), converting it back to a number.
  • I was able to get rid of a variable by adding '.replace()' to the end of '.toString()' and it works


/**
 * @param {number} num
 * @return {number}
 */
var maximum69Number  = function(num) {
    let numString = num.toString().replace("6", "9");
    return parseInt(numString);
};

Approach) Native Solution

/**
 * @param {number} num
 * @return {number}
 */
var maximum69Number  = function(num) {
    const to_add = []
    
    let ans = 0
    let found = false
    
    let factor = 10
    
    while(num >= 6) {
        let rem = num % factor
        to_add.push(rem)
        
        num = parseInt(num / factor) * factor
        factor *= 10
    }
    
    for(let i = to_add.length - 1; i >= 0; --i) {
        let num = to_add[i]
        
        if(!found && num == 6 * Math.pow(10, i)) {
            num = parseInt(num / 6) * 9
            found = true
        }
        
        ans += num
    }
    
    return ans    
}; 

Approach) Javascript One Liner

const maximum69Number = (num, flag = true) => 
  +[...num+''].map(digit => { if(digit == '6' && flag) { flag = false; return '9'} return digit }).join('');

Approach) Math

/**
 * @param {number} decreasing
 * @return {number}
 */
 const maximum69Number = decreasing => {
  // Move the most significant digit from one number to another
  // The number passed in will be `decreasing` and this will be `increasing`
  let increasing = 0;
  // While the decreasing number still has significant digits to be moved
  while (0 < decreasing) {
    // Use a combo of decimal and binary math to get the most significant digit
    // (`x | 0` is like `Math.trunc(x)` but shorter, and binary)
    const sigDigit = (decreasing / 10 ** (Math.log10(decreasing) | 0)) | 0;
    // Use a combo of decimal and binary math to get the power of ten of the
    // most significant digit
    // (Really, `x | 0` is just shorter; that's the only reason it's used)
    const powerOfTen = 10 ** (Math.log10(decreasing) | 0);
    // If the most significant digit is a 6, return the answer
    if (6 === sigDigit) return increasing + decreasing + 3 * powerOfTen;
    // The `decreasing` number loses it's most significant digit
    decreasing -= sigDigit * powerOfTen;
    // And the `increasing` number gets a new least significant non-zero digit
    increasing += sigDigit * powerOfTen;
  }
  // If we got here, no `6`s were found and all digits moved from
  // `decreasing` to `increasing` so we have to return `increasing`
  // to return the original `decreasing` number
  return increasing;
};

OR

const maximum69Number = num => {
  for (let pow = Math.log10(num) | 0; 0 <= pow; pow--)
    if (6 === ((num / 10 ** pow) | 0) % 10) return num + 3 * 10 ** pow;
  return num;
};


Conclusion

That’s all folks! In this post, we solved LeetCode problem 1323. Maximum 69 Number

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!

Comments

Popular Post