[LeetCode] 746. Min Cost Climbing Stairs

LeeKyungwon·2026년 6월 4일

공부 정리

목록 보기
33/34

문제

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

Constraints:

2 <= cost.length <= 1000
0 <= cost[i] <= 999

풀이

var minCostClimbingStairs = function(cost) {
    let dp = [];
    //dp[i] : i번 계단가지 오는 데 걸린 최소 누적 비용
    dp[0] = cost[0];
    dp[1] = cost[1];

    for(let i = 2; i < cost.length; i++){
        dp[i] = cost[i] + Math.min(dp[i-1], dp[i-2]);
    }
    return Math.min(dp[cost.length - 1], dp[cost.length - 2]);
};

0개의 댓글