Skip to content

Commit 44d54ea

Browse files
committed
3 solved
1 parent 13dbc6c commit 44d54ea

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

climbing-stairs/devyulbae.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Blind 75 - LeetCode Problem 70. Climbing Stairs
3+
https://leetcode.com/problems/climbing-stairs/
4+
5+
계단이 n개, 한 번에 1칸 또는 2칸씩 오를 수 있다.
6+
combinations를 썼더니 시간초과 발생 (O(2^n))
7+
8+
DP를 이용해 시간복잡도 O(n) 공간복잡도 O(1)로 풀기
9+
f(n) = f(n-1) + f(n-2)
10+
"""
11+
class Solution:
12+
def climbStairs(self, n: int) -> int:
13+
if n <= 2:
14+
return n
15+
16+
one_before = 2 # n=2
17+
two_before = 1 # n=1
18+
19+
for _ in range(3, n+1):
20+
current = one_before + two_before # f(1) = f(n-1) + f(n-2)
21+
two_before = one_before # f 밀기
22+
one_before = current # f 밀기
23+
24+
return current

0 commit comments

Comments
 (0)