Skip to content

Commit 7b8df80

Browse files
committed
feat: Solve climbing-stairs problem
1 parent 4a966b4 commit 7b8df80

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

β€Žclimbing-stairs/hu6r1s.pyβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
1일 λ•Œ, 1 step -> 1개
4+
2일 λ•Œ, 1 + 1, 2 -> 2개
5+
3일 λ•Œ, 1 + 1 + 1, 1 + 2, 2 + 1 -> 3개
6+
4일 λ•Œ, 1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1, 2 + 2 -> 5개
7+
5일 λ•Œ, 1 + 1 + 1 + 1 + 1, 1 + 1 + 1 + 2, 1 + 1 + 2 + 1, 1 + 2 + 1 + 1, 2 + 1 + 1 + 1, 1 + 2 + 2, 2 + 1 + 2, 2 + 2 + 1 -> 8개
8+
μˆœμ„œλŒ€λ‘œ 봀을 λ•Œ, 이전 κ°’κ³Ό κ·Έ 이전 κ°’μ˜ 합이 ν˜„μž¬ κ°œμˆ˜μ΄λ‹€.
9+
이λ₯Ό μ ν™”μ‹μœΌλ‘œ 봀을 λ•Œ, dp[i] = d[i - 1] + dp[i - 2]κ°€ λœλ‹€.
10+
11+
- Time Complexity: O(n)
12+
- 1λΆ€ν„° nκΉŒμ§€ ν•œ λ²ˆμ”© λ°˜λ³΅ν•˜λ―€λ‘œ μ„ ν˜• μ‹œκ°„
13+
- Space Complexity: O(n)
14+
- dp 배열에 n개의 값을 μ €μž₯ν•˜λ―€λ‘œ μ„ ν˜• 곡간 μ‚¬μš©
15+
"""
16+
def climbStairs(self, n: int) -> int:
17+
if n == 1:
18+
return n
19+
20+
dp = [0] * n
21+
dp[0] = 1
22+
dp[1] = 2
23+
for i in range(2, n):
24+
dp[i] = dp[i - 1] + dp[i - 2]
25+
return dp[-1]

0 commit comments

Comments
Β (0)