# Leetcode 402 - Remove K Digits

Note:

• Intuition: By observing examples, I found that we should remove a num when num[i] > num[i + 1]. In addition, we should start this from the most significant digits.
• Things I overlooked:
• Didn’t consider about the new nums after removing some in-between digits. There might be new digits that meet the patterns num[i] > num[i + 1] after removing some in-between digits.
• Is there a data structure that can be used to constantly pop out digits that are bigger than following digits?
• Yes, it’s monotonic stack!
• Use mono stack while iterating num. Note that every time we use stack.pop(), we need to k--. And we only do pop() while k > 0.
• Be careful when there are leading zeroes in the front.

Question:

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:

Code: