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

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate