We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 13dbc6c commit 44d54eaCopy full SHA for 44d54ea
climbing-stairs/devyulbae.py
@@ -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