Created
May 11, 2019 16:23
-
-
Save priyankvex/77b6b20cfba4f1c53d112bf6deeff5e7 to your computer and use it in GitHub Desktop.
Remove K digits to find the smallest number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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