Monotone Increasing Digits

Problem Id: 738 Difficulty: Medium


Intuition

Solution


class Solution:
    def monotoneIncreasingDigits(self, N: int) -> int:
        digits = []
        A = list(map(int, str(N)))
        for i in range(len(A)):
            for digit in range(1, 10):
                if digits + [digit] * (len(A) - i) > A:
                    digits.append(digit - 1)
                    break
            else:
                digits.append(9)
        return int("".join(map(str, digits)))