The greedy approach clicked pretty fast: scan from the right, and whenever you see a digit that's smaller than the one before it, decrement the left digit by 1 and set everything to its right to 9.
Start by clarifying the problem and edge cases, then propose a greedy algorithm that scans digits from left to right, and when a decrease is found, decrement the previous digit and set all following digits to 9. After the scan, fix any new violations by propagating the decrement leftward, and finally remove leading zeros.
Pro tip: Mention that the greedy approach works because we want the largest number, so we only decrease a digit when forced, and setting subsequent digits to 9 maximizes the value. Also, discuss how to handle large numbers as strings to avoid integer overflow.
Ask about constraints (e.g., n up to 10^9 or larger), negative numbers, and single-digit numbers. Confirm that monotone increasing means non-decreasing (each digit ≤ next).
Convert n to a string or digit array. Scan from left to right; when you find a digit greater than the next, decrement the current digit and set all following digits to 9.
After the initial change, the decremented digit might now be less than the previous digit. Propagate the decrement leftward until the sequence is non-decreasing.
Convert the digit array back to a number, stripping any leading zeros. If the result is empty, return 0.
Test with examples like n=10 (result 9), n=1234 (result 1234), n=332 (result 299), and edge cases like n=0 or n=100.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.