Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created January 30, 2015 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanplopes/e8d92a70609a5321290d to your computer and use it in GitHub Desktop.
Save juanplopes/e8d92a70609a5321290d to your computer and use it in GitHub Desktop.
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