#402

Remove K Digits

Medium Monotonic increasing stack

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Remove k digits from a number string to make the smallest possible number. Return it without leading zeros ("0" if empty).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: remove k digits to make the smallest number.

Why this works: a digit followed by a smaller digit should go. Keep the kept digits increasing with a stack.

Example: "1432219", k = 3

4 removed by 3, 3 removed by 2, 2 removed by 1"1219"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting leading zeros or the "0" for an empty result.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon