Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.
Example 1:
Input: n = 10 Output: 9
Example 2:
Input: n = 1234 Output: 1234
Example 3:
Input: n = 332 Output: 299
Constraints:
0 <= n <= 109Problem summary: An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y. Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
10
1234
332
remove-k-digits)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #738: Monotone Increasing Digits
class Solution {
public int monotoneIncreasingDigits(int n) {
char[] s = String.valueOf(n).toCharArray();
int i = 1;
for (; i < s.length && s[i - 1] <= s[i]; ++i)
;
if (i < s.length) {
for (; i > 0 && s[i - 1] > s[i]; --i) {
--s[i - 1];
}
++i;
for (; i < s.length; ++i) {
s[i] = '9';
}
}
return Integer.parseInt(String.valueOf(s));
}
}
// Accepted solution for LeetCode #738: Monotone Increasing Digits
func monotoneIncreasingDigits(n int) int {
s := []byte(strconv.Itoa(n))
i := 1
for ; i < len(s) && s[i-1] <= s[i]; i++ {
}
if i < len(s) {
for ; i > 0 && s[i-1] > s[i]; i-- {
s[i-1]--
}
i++
for ; i < len(s); i++ {
s[i] = '9'
}
}
ans, _ := strconv.Atoi(string(s))
return ans
}
# Accepted solution for LeetCode #738: Monotone Increasing Digits
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
s = list(str(n))
i = 1
while i < len(s) and s[i - 1] <= s[i]:
i += 1
if i < len(s):
while i and s[i - 1] > s[i]:
s[i - 1] = str(int(s[i - 1]) - 1)
i -= 1
i += 1
while i < len(s):
s[i] = '9'
i += 1
return int(''.join(s))
// Accepted solution for LeetCode #738: Monotone Increasing Digits
struct Solution;
impl Solution {
fn monotone_increasing_digits(n: i32) -> i32 {
let mut s: Vec<char> = n.to_string().chars().collect();
let n = s.len();
let mut i = 1;
while i < n && s[i] >= s[i - 1] {
i += 1;
}
while i > 0 && i < n && s[i - 1] > s[i] {
s[i - 1] = (s[i - 1] as u8 - 1) as char;
i -= 1;
}
while i + 1 < n {
s[i + 1] = '9';
i += 1;
}
s.into_iter().collect::<String>().parse::<i32>().unwrap()
}
}
#[test]
fn test() {
let n = 10;
let res = 9;
assert_eq!(Solution::monotone_increasing_digits(n), res);
let n = 1234;
let res = 1234;
assert_eq!(Solution::monotone_increasing_digits(n), res);
let n = 332;
let res = 299;
assert_eq!(Solution::monotone_increasing_digits(n), res);
}
// Accepted solution for LeetCode #738: Monotone Increasing Digits
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #738: Monotone Increasing Digits
// class Solution {
// public int monotoneIncreasingDigits(int n) {
// char[] s = String.valueOf(n).toCharArray();
// int i = 1;
// for (; i < s.length && s[i - 1] <= s[i]; ++i)
// ;
// if (i < s.length) {
// for (; i > 0 && s[i - 1] > s[i]; --i) {
// --s[i - 1];
// }
// ++i;
// for (; i < s.length; ++i) {
// s[i] = '9';
// }
// }
// return Integer.parseInt(String.valueOf(s));
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.