-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0402 [M] Remove K Digits
Code with Senpai edited this page Jun 2, 2022
·
2 revisions
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'
footer