Talentd
FleetCode

738. Monotone Increasing Digits

Medium50.1% acceptance3 companies

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:

Input: n = 10
Output: 9

Example 2:

Input: n = 1234
Output: 1234

Example 3:

Input: n = 332
Output: 299

Constraints:

  • 0 <= n <= 109

Asked at 3 companies

Hints open one at a time

Hint 1
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
18:08
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution

Similar problems

Remove K DigitsMedium
Loading editor…
1class Solution:
2 def monotoneIncreasingDigits(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
10
Input
1234
Input
332
Your code is saved on this device.Solution