Skip to content

Commit 82cfd90

Browse files
committed
climbing-stairs sol (py)
1 parent 1c489a3 commit 82cfd90

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
https://leetcode.com/problems/climbing-stairs/description/
3+
4+
ํ•œ ๋ฒˆ์— 1๊ณ„๋‹จ ํ˜น์€ 2๊ณ„๋‹จ ์˜ค๋ฅผ ์ˆ˜ ์žˆ์Œ
5+
1: 1
6+
2: 2
7+
3: 1 + 1 + 1, 1 + 2, 2 + 1 => 3
8+
4: 1 + 1 + 1 + 1, 1 + 1 + 2, 2 + 1 + 1, 1 + 2 + 1, 2 + 2 => 5
9+
5: 1 + 1 + 1 + 1 + 1,
10+
1 + 1 + 1 + 2,
11+
1 + 1 + 2 + 1 + 1,
12+
1 + 2 + 1 + 1,
13+
2 + 1 + 1 + 1,
14+
2 + 2 + 1,
15+
2 + 1 + 2,
16+
1 + 2 + 2,
17+
=> 8
18+
19+
steps[n] = steps[n - 1] + steps[n - 2]
20+
21+
TC: O(n)
22+
SC: O(n)
23+
"""
24+
25+
class Solution:
26+
def climbStairs(self, n: int) -> int:
27+
if n == 1:
28+
return 1
29+
elif n == 2:
30+
return 2
31+
steps = [0] * n
32+
steps[0] = 1
33+
steps[1] = 2
34+
for i in range(2, n):
35+
steps[i] = steps[i - 2] + steps[i - 1]
36+
return steps[n - 1]

0 commit comments

Comments
ย (0)