Remove K Digits
Remove K Digits is LeetCode 402 (Medium). Given a non-negative integer as the string num and an integer k, delete exactly k digits so the number left is as small as possible, and return it as a string.
- The remaining digits keep their order.
- Drop any leading zeros from the answer, and return
"0"if nothing is left. numcan have 10⁵ digits, so it cannot be turned into a normal integer, and trying every set of deletions is out of the question.
- 1 <= k <= num.length <= 10⁵
- num consists of only digits.
- num does not have any leading zeros except for the zero itself.
Intuition
Every answer has the same length, n - k, so the smaller one is the one with the smaller digit at the first position where they differ. The leftmost digits matter most.
If a digit is bigger than the digit right after it, deleting it makes the number smaller. The digits before it stay, and its place is taken by a smaller digit. Doing this as far left as possible is always best.
A stack finds these digits in one pass. Before pushing a new digit, pop every larger digit on top while deletions remain. The stack then stays non-decreasing from bottom to top.
Approach
Scan the digits with a stack
For each digit d, while k > 0 and the top of the stack is larger than d, pop it and subtract 1 from k. Then push d. Each pop removes a digit that a smaller digit was about to follow.
Spend leftover deletions at the end
If k is still above 0, the stack is non-decreasing, so its largest digits are on top. Cut the last k digits; any other cut would move a bigger digit to the left.
Strip leading zeros
Remove zeros from the front of what is left. A pop can leave them at the front, and they add nothing to the value, so the answer is written without them.
Return "0" for an empty result
If every digit was deleted or every digit left was a zero, the string is empty. The smallest number is then zero, written "0".
Code in Python, C++ and Java
Common pitfalls
Forgetting deletions left after the scan
return "".join(stack).lstrip("0") or "0"del stack[len(stack) - k:]
return "".join(stack).lstrip("0") or "0"In a rising number like "12345" with k = 2, nothing is ever popped. Without the trim the answer is "12345" instead of "123".
Popping without checking k
while stack and stack[-1] > d:
stack.pop()
k -= 1while k and stack and stack[-1] > d:
stack.pop()
k -= 1The loop keeps popping after the budget is spent. "4321" with k = 1 pops all three larger digits and returns "1", but only one digit may go, so the answer is "321".
Edge cases
Every digit is deleted, the string is empty, and the answer is "0".
Popping the 1 leaves "0200". Stripping gives "200", not "0200".
Complexity
Remove K Digits FAQ
Is deleting one digit at a time, k times, also correct?
Yes. Each round scans for the first digit that is bigger than the one after it (or the last digit) and deletes it. That is the same greedy rule, but each round restarts from the left, so it costs O(n·k), up to about 10¹⁰ steps here.
How would I get the largest number instead?
Flip the comparison: pop while the top is smaller than the new digit. The stack then stays non-increasing, and leftover deletions still come off the end.
Which problems use the same stack idea?
Remove Duplicate Letters keeps a rising stack of letters, but it may only pop a letter that appears again later, instead of spending a budget of k deletions.