Monotone Increasing Digits - Problem
A number has monotone increasing digits if each digit is less than or equal to the next digit when reading from left to right. For example, 1234 and 1333 have monotone increasing digits, but 4321 and 1232 do not.

Given an integer n, your task is to find the largest number that is less than or equal to n and has monotone increasing digits. This is a classic greedy algorithm problem where we need to make optimal local choices to achieve the global maximum.

Key Challenge: When we encounter a digit that violates the monotone property (i.e., a digit is greater than the next digit), we need to strategically reduce it and set all subsequent digits to 9 to maximize the result.

Input & Output

example_1.py โ€” Python
$ Input: n = 10
โ€บ Output: 9
๐Ÿ’ก Note: The number 10 has digits [1, 0], where 1 > 0, so it doesn't have monotone increasing digits. The largest number โ‰ค 10 with monotone increasing digits is 9.
example_2.py โ€” Python
$ Input: n = 1332
โ€บ Output: 1299
๐Ÿ’ก Note: The number 1332 has digits [1, 3, 3, 2]. Since 3 > 2, we need to fix this. We decrement the first 3 to get 2, then set all following digits to 9, resulting in 1299.
example_3.py โ€” Python
$ Input: n = 54321
โ€บ Output: 49999
๐Ÿ’ก Note: This number has multiple violations (5>4>3>2>1). Starting from right, we find 2>1, fix it to get 54319. Then 3>1, fix to get 54299. Continue this process until we get 49999.

Constraints

  • 0 โ‰ค n โ‰ค 109
  • The result will always fit in a 32-bit integer
  • Time limit: 1 second per test case

Visualization

Tap to expand
The Perfect Staircase: Monotone Increasing DigitsOriginal: 1332 (Invalid Staircase)Step 11Step 23Step 33Step 42โŒ Step 3 height > Step 4 heightAfter Greedy Fix: 1299 (Valid Staircase)Step 11Step 22Step 39Step 49โœ“ Perfect monotone increasing staircase!Greedy Strategy1. Scan right-to-left for violations2. Decrement violating step by 13. Set all following steps to max (9)4. Result: Largest valid staircase!Time ComplexityO(d) where d = number of digitsSpace: O(d) for digit storage
Understanding the Visualization
1
Inspect the Blueprint
Start with your target staircase design (the input number)
2
Scan for Problems
Check from right to left for steps that violate the height rule
3
Fix Violations
When you find a step that's too high, lower it by one unit
4
Maximize Remaining
Set all steps to the right to maximum height (9) to get the tallest valid staircase
Key Takeaway
๐ŸŽฏ Key Insight: Work backwards from right to left. When you find a violation, make the minimal fix (decrement by 1) and maximize everything to the right (set to 9). This greedy approach guarantees the largest possible valid result.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 25
42.0K Views
Medium Frequency
~18 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen