Largest Tidy Number Below
A number is *tidy* when its decimal digits never decrease from left to right: 1337, 55 and every single-digit number are tidy, while 321 and 100 are not.
Return the largest tidy number that is less than or equal to n. Since every single-digit number (including 0) is tidy, an answer always exists, and it is unique.
Example 1
n 332
returns 299
332 itself is not tidy; lowering the leading digits to 2 and filling with 9s gives 299, and no tidy number between 300 and 332 exists.
Example 2
n 1234
returns 1234
1234 is already tidy, so it is its own answer.
Constraints
0 <= n <= 1000000000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.