Skip to content

Commit 0078452

Browse files
committed
746. Min Cost Climbing Stairs
1 parent 687d5fe commit 0078452

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
3+
1. 아이디어 : 계단 1칸 or 2칸 오르고, 최소비용 구하기
4+
5+
dp로 직전위치까지의 최소 비용 계산후 값 반환
6+
7+
2. 시간복잡도 : O(N)
8+
9+
3. 자료구조/알고리즘 : DP
10+
11+
*/
12+
13+
class Solution {
14+
public int minCostClimbingStairs(int[] cost) {
15+
// 1개 or 2개
16+
17+
int[] dp = new int[cost.length+1];
18+
19+
dp[1] = cost[0];
20+
dp[2] = cost[1];
21+
22+
for(int i=3; i<cost.length+1; i++) {
23+
dp[i] = cost[i-1] + Math.min(dp[i-1], dp[i-2]);
24+
}
25+
26+
int ans = Math.min(dp[cost.length], dp[cost.length-1]);
27+
28+
return ans;
29+
}
30+
}

0 commit comments

Comments
 (0)