Skip to content

Commit f00520e

Browse files
committed
feat: [Week 02-2] solve climbing-stairs
1 parent 9fcc0bc commit f00520e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

climbing-stairs/Chaedie.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
solution:
3+
# dp[1] = 1step = 1
4+
# dp[2] = (1step + 1step) + 2step = 2
5+
# dp[3] = (dp[3 - 1] + 1step) + dp[3 - 2] + 2step = 2 + 1 = 3
6+
# dp[4] = (dp[4 - 1] + 1step) + (dp[4 - 2] + 2tep) = 3 + 2 = 5
7+
8+
# dp[n] = (dp[n-1] + 1) + (dp[n-2] + 1)
9+
10+
time O(n)
11+
space O(n)
12+
13+
'''
14+
15+
class Solution:
16+
def climbStairs(self, n: int) -> int:
17+
dp = [0]
18+
dp.append(1)
19+
dp.append(2)
20+
21+
for i in range(3, n+1):
22+
dp.append(dp[i-1] + dp[i-2])
23+
return dp[n]

0 commit comments

Comments
 (0)