Skip to content

Create climbing_stairs.py #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions dynamic_programming/climbing_stairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def climb_stairs(n: int) -> int:
"""
LeetCdoe No.70: Climbing Stairs
Distinct ways to climb a n step staircase where
each time you can either climb 1 or 2 steps.

Args:
n: number of steps of staircase

Returns:
Distinct ways to climb a n step staircase

Raises:
AssertionError: n not positive integer

>>> climb_stairs(3)
3
>>> climb_stairs(1)
1
"""
assert isinstance(n,int) and n > 0, "n needs to be positive integer, your input {0}".format(0)
Copy link
Member

@cclauss cclauss Jul 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the zero inside the {} and you can replace the last zero with n.

if n == 1: return 1
dp = [0]*(n+1)
dp[0], dp[1] = (1, 1)
for i in range(2,n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]