738. Monotone Increasing Digits

Source code notebook Author Update time

Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits.

(Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.)

Example 1:

Input: N = 10
Output: 9

Example 2:

Input: N = 1234
Output: 1234

Example 3:

Input: N = 332
Output: 299

Note: N is an integer in the range [0, 10^9].

# @lc code=start
using LeetCode

function monotone_increasing_digits(N::Int)::Int
    some_ones = 111111111
    res = 0
    for _ in 1:9
        while res + some_ones > N
            some_ones ÷= 10
        end
        res += some_ones
    end
    return res
end

# @lc code=end
monotone_increasing_digits (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.