Problem 474
Monotone Increasing Digits
Medium · 20 pts · Math, Greedy
Problem
A number has monotone increasing digits if each digit is less than or equal to the digit after it, like 1234 or 1299. Given N, find the largest number that is at most N and has monotone increasing digits. Scan the digits from the right: whenever a digit is larger than the next one, decrease it by one and set every digit after it to 9.
Input
A single integer N.
Output
Print the largest number <= N with monotone increasing digits.
Constraints
0 <= N <= 1000000000
Example input
10
Example output
9
Explanation
10 does not qualify (1 > 0), but 9 does, and nothing between 9 and 10 qualifies, so the answer is 9.