Skip to content

Commit 9024ec5

Browse files
committed
Solve climbing-stairs with python
1 parent 89b1809 commit 9024ec5

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

climbing-stairs/bemelon.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# Space complexity: O(1)
3+
# Tiem complexity: O(n)
4+
def climbStairs(self, n: int) -> int:
5+
# dp[0] is n - 2
6+
# dp[1] is n - 1
7+
dp = [1, 2]
8+
9+
if n <= 2:
10+
return dp[n - 1]
11+
12+
for i in range(3, n + 1):
13+
# dp[n] = dp[n - 1] + dp[n - 2]
14+
# = dp[1] + dp[0]
15+
dp[(i - 1) % 2] = sum(dp)
16+
17+
return dp[(n - 1) % 2]

0 commit comments

Comments
 (0)