738. Monotone Increasing Digits

https://leetcode.com/problems/monotone-increasing-digits/

solution

  • 暴力法

    • 注意如何操作位数之间

class Solution:
    def monotoneIncreasingDigits(self, n: int) -> int:
        for i in range(n, 0, -1):
            if self.check_mono(i):
                return i
        return 0

    def check_mono(self, num):
        m = 10
        while num > 0:
            x = num % 10
            if m >= x:
                m = x
            else:
                return False
            num = num // 10
        return True

时间复杂度:O() 空间复杂度:O()

  • 贪心

    • 根据规律人工剪枝,如果小于上一位,上一位减1,这一位变9. 从后往前变量利用之前结果

时间复杂度:O() 空间复杂度:O()

follow up

31. Next Permutation

670. Maximum Swap

时间复杂度:O() 空间复杂度:O()

时间复杂度:O(n) 空间复杂度:O(n)

Last updated