Skip to content

Commit 2415f2d

Browse files
committed
climbing-stairs solution
1 parent 1e8d5cf commit 2415f2d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
- ๋‹ฌ๋ ˆ์Šคํ„ฐ๋”” ํ’€์ด ์ฐธ๊ณ ํ•จ
3+
ํ’€์ด :
4+
n๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ธฐ ์œ„ํ•ด์„œ๋Š” n-1 ๋˜๋Š” n-2์— ์žˆ๋‹ค๊ฐ€ ์˜ฌ๋ผ์˜ค๋Š” ์ˆ˜ ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ f(n-1) + f(n-2)์˜ ๊ฐ’
5+
dp ๋ฐฐ์—ด๋กœ ํ’€ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜์— ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ์‹์œผ๋กœ ํ’€์ด
6+
7+
TC :
8+
for๋ฌธ์œผ๋กœ ์ธํ•ด O(N)
9+
10+
SC :
11+
O(1)
12+
'''
13+
14+
class Solution:
15+
def climbStairs(self, n: int) -> int:
16+
if n < 3 :
17+
return (n)
18+
prv, cur = 1, 2
19+
for _ in range(3, n + 1) :
20+
prv, cur = cur, prv + cur
21+
return (cur)

0 commit comments

Comments
ย (0)