Skip to content

Commit cbe4aeb

Browse files
committed
climbing-stairs solution
1 parent 6c2971e commit cbe4aeb

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
"""
4+
n๋ฒˆ์งธ ๊ณ„๋‹จ์— ๋„๋‹ฌํ•˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
5+
ํ•œ ๋ฒˆ์— 1์นธ ๋˜๋Š” 2์นธ์”ฉ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ DP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ dp[i] = dp[i-1] + dp[i-2] ์ ํ™”์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
6+
์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณ„๋‹จ์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉฐ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
7+
"""
8+
dp = [0] * (n + 2)
9+
dp[1] = 1
10+
dp[2] = 2
11+
for i in range(3, n+1):
12+
dp[i] = dp[i-1] + dp[i-2]
13+
return dp[n]

0 commit comments

Comments
ย (0)