{x}
blog image

Monotone Increasing Digits

An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.

Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.

 

Example 1:

Input: n = 10
Output: 9

Example 2:

Input: n = 1234
Output: 1234

Example 3:

Input: n = 332
Output: 299

 

Constraints:

  • 0 <= n <= 109

Solution Explanation for Monotone Increasing Digits

The problem asks to find the largest number less than or equal to a given integer n that has monotone increasing digits (each digit is less than or equal to the next).

Approach:

The solution uses a greedy approach. It iterates through the digits of the input number from left to right. If it finds a digit that is smaller than the previous digit, it means the monotone increasing property is violated. To correct this, it decreases the violating digit by 1 and sets all subsequent digits to 9. This ensures that the resulting number is the largest possible monotone increasing number less than or equal to the input.

Algorithm:

  1. Convert to String: Convert the input integer n into a string s for easier digit manipulation.

  2. Iteration and Check: Iterate through the string s from the second digit (index 1). At each step, compare the current digit with the previous digit.

  3. Violation Detected: If a digit is smaller than its preceding digit (violating the monotone increasing property), enter the correction phase.

  4. Correction Phase:

    • Decrement the violating digit by 1.
    • Iterate backward from the violating digit's position. Set all subsequent digits to '9'. This maximizes the number while maintaining the monotone increasing property.
  5. Conversion back to Integer: Convert the modified string back to an integer and return it.

Time Complexity Analysis:

The algorithm iterates through the digits of the number at most twice (once for the initial check and once for the correction phase in the worst case). The number of digits is logarithmic with respect to the input number (O(log n)). Therefore, the overall time complexity is O(log n).

Space Complexity Analysis:

The space used is primarily for storing the string representation of the number. The length of this string is proportional to the number of digits (O(log n)). Therefore, the space complexity is O(log n).

Code Explanation with Examples (Python):

class Solution:
    def monotoneIncreasingDigits(self, n: int) -> int:
        s = list(str(n))  # Convert to list of strings for easy modification
        i = 1
        while i < len(s) and s[i - 1] <= s[i]: # Find the first violation
            i += 1
 
        if i < len(s):  # Violation found
            while i > 0 and s[i - 1] > s[i]: # Correct the violation
                s[i - 1] = str(int(s[i - 1]) - 1)
                i -= 1
            i += 1  # Move to the next digit
            while i < len(s): #Set remaining digits to 9
                s[i] = '9'
                i += 1
        return int(''.join(s)) #Convert back to integer
 
 
#Example 1
n = 332
solution = Solution()
result = solution.monotoneIncreasingDigits(n)
print(f"Largest monotone increasing digit for {n}: {result}") # Output: 299
 
# Example 2
n = 1234
result = solution.monotoneIncreasingDigits(n)
print(f"Largest monotone increasing digit for {n}: {result}")  # Output: 1234
 
# Example 3
n = 10
result = solution.monotoneIncreasingDigits(n)
print(f"Largest monotone increasing digit for {n}: {result}") # Output: 9
 

The other languages (Java, C++, Go) follow the same logic with minor syntactic differences. The core algorithm remains consistent across all implementations.