Created
August 28, 2018 09:45
-
-
Save woodRock/779cdd72d473ab96a0f753cc5e76d605 to your computer and use it in GitHub Desktop.
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters. For example, if N is 4, then there are 5 unique ways: 1, 1, 1, 1 2, 1, 1 1, 2, 1 1, 1, 2 2, 2 What if, instead of being able to cl…
This file contains hidden or 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
#!/usr/bin/env python | |
def staircase(n, X): | |
cache = [0 for _ in range(n + 1)] | |
cache[0] = 1 | |
for i in range(n + 1): | |
cache[i] += sum(cache[i - x] for x in X if i - x > 0) | |
cache[i] += 1 if i in X else 0 | |
return cache[-1] | |
if __name__ == "__main__": | |
X = {1, 3, 5} | |
print (staircase(10, X)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment