Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save priyankvex/77b6b20cfba4f1c53d112bf6deeff5e7 to your computer and use it in GitHub Desktop.
Save priyankvex/77b6b20cfba4f1c53d112bf6deeff5e7 to your computer and use it in GitHub Desktop.
Remove K digits to find the smallest number
"""
https://scammingthecodinginterview.com
Week 3: Greedy
Problem: 3
"""
class Solution(object):
def solve(self, a, k):
out = []
for num in a:
while k and out and out[-1] > num:
out.pop()
k -= 1
out.append(num)
return ''.join(out[:-k or None]).lstrip('0') or '0'
if __name__ == "__main__":
a = "1432219"
k = 3
ans = Solution().solve(a, k)
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment