← 返回 google 的题目列表Monotone Increasing Digits
类型:online_judge
Given a non-negative integer n, return the largest integer x such that x <= n and the decimal digits of x are monotone increasing.
A number has monotone increasing digits if every digit is less than or equal to the next digit from left to right, i.e. for every adjacent pair a[i] and a[i+1], a[i] <= a[i+1].
In the interview, the original warm-up may have been a “no digit replacement” version, such as checking whether a number is already monotone increasing. The follow-up corresponds to LeetCode 738, where you need to construct the largest valid x.
Input
An integer n
Output
An integer representing the largest monotone increasing number less than or equal to n.
Constraints
0 <= n <= 10^9
Examples
Example 1
Input:
10
Output:
9
Example 2
Input:
1234
Output:
1234
Example 3
Input:
332
Output:
299
Example
Input
10
Output
9