Skip to content

Commit 6fe30d9

Browse files
committed
climbing stairs solution
1 parent 9aa35f8 commit 6fe30d9

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

climbing-stairs/jylee2033.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
# ways(n) = ways(n-1) + ways(n-2)
4+
5+
if n == 1:
6+
return 1
7+
elif n == 2:
8+
return 2
9+
10+
ways = [0] * (n + 1)
11+
ways[1] = 1
12+
ways[2] = 2
13+
14+
for i in range(3, n + 1):
15+
ways[i] = ways[i - 1] + ways[i - 2]
16+
17+
return ways[n]
18+
19+
# Time Complexity: O(n)
20+
# Space Complexity: O(n)

0 commit comments

Comments
 (0)