Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.
Java
Remove K Digits
Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.
Example 1:
Input: {"num":"1432219","k":3}
Output: "1219"
Input
arr ={"num":"1432219","k":3}
Initialize stack, need to remove 3 digits from "1432219"
Input: num = "1432219", k = 3
1
4
3
2
2
1
9
0
1
2
3
4
5
6
Monotonic Stack
Remaining Removals:
3
Current Stack Value:
""
Algorithm Insight:
Use a monotonic increasing stack. When we see a smaller digit, pop larger digits from stack (if we still have removals left). This ensures the result is as small as possible.