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.

30:00

Remove K Digits
medium
Topics
Companies

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
Constraints:
  • 1knum.length1051 \leq k \leq \text{num.length} \leq 10^5

  • num consists of digits only.

  • num does not have leading zeros except for the zero itself.

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.

Variables
No variables to display
DepthFunction Call
Stack empty
0/22