LC 0402 [M] Remove K Digits - ALawliet/algorithms GitHub Wiki
increasing monotonic stack
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
stack = []
for x in num:
while stack and k and stack[-1] > x:
stack.pop()
k -= 1
if stack or x is not '0': # prevent leading zeros
stack.append(x)
if k: # not fully spent
stack = stack[0:-k]
return ''.join(stack) or '0'