Skip to content

Commit 4d1ba6d

Browse files
committed
feat: add "Climbing Stairs" solution
1 parent 110fff4 commit 4d1ba6d

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

climbing-stairs/shinsj4653.py

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,96 @@
11
"""
2-
Inputs:
2+
계단 오르기
3+
맨 꼭대기 가는데 n steps만큼 걸림
34
4-
Outputs:
5+
매번 올라가는데 1 or 2 계단 오르기 가능!
56
6-
Constraints:
7+
Inputs: n
78
8-
Time Complexity:
9+
Outputs: how many distinct ways to get to the top?
910
10-
Space Complexity:
11+
Constraints: 1 <= n <= 45
12+
13+
Time Complexity: O(2^n) ?
14+
15+
계단 오르는 방법 중, 중복되지 않는 모든 가지 수 구하기
16+
우선 완탐으로 해보고, 그 다음 최적부분구조 할 수 있는지 체크
17+
18+
n = 2
19+
20+
1 1 -> dp[1]
21+
2 0 -> dp
22+
23+
2 dp(n - 2) + dp(n - 1) + 1
24+
25+
n = 3
26+
2 1
27+
1 1 1 => dp[2] 값
28+
29+
1 2 => 여기선 dp[2] 로 가면 안됨!
30+
31+
1 2
32+
1 1 1 => dp[2] 값
33+
34+
2 1 => 이건 dp[1] 값
35+
36+
37+
n = 4
38+
1 3 => dp[3]
39+
2 2 => dp[2]
40+
41+
n = 5
42+
2 2 1
43+
2 1 2
44+
1 2 2
45+
46+
n = 6
47+
48+
5 1
49+
4 2
50+
51+
n = 7
52+
53+
6 1
54+
5 4
55+
4 3
56+
57+
특정 수를 구성하는 1과 2의 배열 가짓수들이 정해져있음!!
58+
59+
한 호출마다 뻗어지는 가짓수, 즉 호출수를 모르겠어서 시간복잡도 모르겠음
60+
61+
점화식을 어떻게 세우지?
62+
63+
3
64+
1 2
65+
66+
기저조건또 헷갈... n 3 // 2 + 1
67+
68+
Space Complexity: O(n)
69+
dp 배열 n만큼의 크기 지님
1170
1271
"""
1372

73+
74+
class Solution:
75+
def climbStairs(self, n: int) -> int:
76+
77+
if n == 1:
78+
return 1
79+
80+
dp = [0] * (n + 1)
81+
dp[0] = 1
82+
dp[1] = 1
83+
84+
def climb(n):
85+
86+
if dp[n]:
87+
return dp[n]
88+
89+
else:
90+
dp[n] += climb(n - 1) + climb(n - 2)
91+
return dp[n]
92+
93+
return climb(n)
94+
95+
# sol = Solution()
96+
# sol.climbStairs(3)

0 commit comments

Comments
 (0)