Remove K Digits
Given a non-negative integer represented as a string num and an integer k, remove exactly k digits so that the resulting number is the smallest possible. Return the result as a string with no leading zeros, or "0" if the result is empty.
Open official problem prompt ↗Delete exactly k digits, keeping relative order, so the remaining digits form the smallest possible number.
Like editing a mountain-range skyline down to a valley: whenever a tall peak is immediately followed by a lower point, you knock the peak down first because lower-early is smaller.
- Input
- num = "1432219", k = 3
- Output
- "1219"
- Why
- Removing the digits 4, 3, and the second 2 leaves 1219, the smallest number reachable by deleting three digits.
1 <= k <= num.length <= 10^5num consists of only digitsnum does not have any leading zeros except for the value 0 itself