Skip to content

Commit 6211b14

Browse files
committed
feat: solve climbing-stairs
1 parent 3320013 commit 6211b14

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

climbing-stairs/pmjuu.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
# dp[i] represents the number of distinct ways to climb to the ith step.
4+
# Base cases:
5+
# - There is 1 way to reach step 0 (doing nothing).
6+
# - There is 1 way to reach step 1 (a single step).
7+
dp = [0] * (n + 1)
8+
dp[0], dp[1] = 1, 1
9+
10+
for i in range(2, n + 1):
11+
dp[i] = dp[i - 1] + dp[i - 2]
12+
13+
return dp[n]
14+
15+
# Complexity
16+
# - time: O(n)
17+
# - space: O(n)

0 commit comments

Comments
 (0)