Created
January 30, 2015 16:42
-
-
Save juanplopes/e8d92a70609a5321290d to your computer and use it in GitHub Desktop.
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
def solve(number, k): | |
n = len(number) | |
stack = [] | |
for i, digit in enumerate(str(number)): | |
while stack and digit < stack[-1] and n-i+len(stack) > k: | |
stack.pop() | |
if len(stack) < n - k: | |
stack.append(digit) | |
return int(''.join(stack[:k])) | |
print solve('24635', 2) | |
print solve('123456', 2) | |
print solve('654321', 2) | |
print solve('122212221', 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment