Mid/SeniorGreedyMath

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
Inputn = 10
Output9
The largest number less than or equal to 10 with monotone increasing digits is 9.
Example 2
Inputn = 1234
Output1234
The digits of 1234 are already monotone increasing, so the answer is 1234.

Constraints

  • 0 <= n <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate