# Solving Leetcode Interviews in Seconds with AI: Monotone Increasing Digits


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "738" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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  

	# Explanation
	Here's the breakdown of the problem and the optimal solution:

*   **High-Level Approach:**
    *   Iterate from the most significant digit to the least significant digit.
    *   If a digit is greater than the digit to its right, decrement the digit and change all subsequent digits to 9.
    *   Handle potential carry-over effects of decrementing a digit (e.g., 10 -> 9, leading to 09) by propagating the decrement to the left.

*   **Complexity:**
    *   Runtime: O(log n) - proportional to the number of digits in `n`.
    *   Storage: O(log n) - to store the digits of `n`.

	
	# Code
	```python
	def monotoneIncreasingDigits(n: int) -> int:
    s = list(str(n))
    length = len(s)

    for i in range(length - 1, 0, -1):
        if s[i] < s[i - 1]:
            s[i - 1] = str(int(s[i - 1]) - 1)
            for j in range(i, length):
                s[j] = '9'

    # Handle leading zeros after decrementing.  This is crucial!  Without this the solution will be incorrect.
    i = 0
    while i < length - 1 and s[i] == '0':
        i += 1

    return int("".join(s[i:]))
	```
			
