[Python] short stack solution, explained
The idea is to build the biggest number using stack and greedy algorithm. Traverse all digits from num and for each of them check if it is worth to remove first
The idea is to build the biggest number using stack and greedy algorithm. Traverse all digits from num and for each of them check if it is worth to remove first some last digits from our stack: if we removed digit, decrease attempt (equal to `k` at the beginning) by `1`. In the end we possibly need to remove some last symbols, so overall we have `k` removed symbols and also remove all leading zeroes.\n\n#### Complexity\nIt is `O(n)` for time and space.\n\n#### Code\n```python\nclass Solution:\n    def removeKdigits(self, num, k):\n        attempts, stack = k, []\n        \n        for digit in num:\n            while stack and digit < stack[-1] and attempts > 0:\n                stack.pop()\n                attempts -= 1\n            stack.append(digit)\n        \n        out = "".join(stack[0:len(num)-k]).lstrip("0")\n        return "0" if out == "" else out \n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
['Stack', 'Greedy']