1 parent 33883fc commit 043da94Copy full SHA for 043da94
1 file changed
leetcode3/변지협/v3/746. Min Cost Climbing Stairs.py
@@ -0,0 +1,24 @@
1
+'''
2
+1. 아이디어 :
3
+ 계단을 오르는 최소 비용. i번째 계단까지 올라오는 최소 비용을 dp[i]로 두면,
4
+ i번째 계단은 i-1 또는 i-2에서 올 수 있으므로
5
+ dp[i] = min(dp[i-1], dp[i-2]) + cost[i].
6
+ 마지막 계단(n-1)에 도착하지 않고 n-1을 건너뛸 수도 있으므로,
7
+ 정답은 마지막 두 칸 중 더 작은 값인 min(dp[n-1], dp[n-2]).
8
+2. 시간복잡도 :
9
+ o(n)
10
+3. 자료구조/알고리즘 :
11
+ dp, bottom-up
12
13
+
14
+class Solution:
15
+ def minCostClimbingStairs(self, cost: list[int]) -> int:
16
+ n = len(cost)
17
+ dp = [0] * n
18
19
+ dp[0] = cost[0]
20
+ dp[1] = cost[1]
21
+ for i in range(2, n):
22
+ dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
23
24
+ return min(dp[n-1], dp[n-2])
0 commit comments