746. Min Cost Climbing Stairs

양성준·2025년 6월 30일

코딩테스트

목록 보기
86/102

문제

https://leetcode.com/problems/min-cost-climbing-stairs/

풀이

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n];

        if(n == 0) return 0;
        if(n == 1) return cost[0];

        dp[0] = cost[0];
        dp[1] = cost[1];

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

        return Math.min(dp[n-1], dp[n-2]);
    }
}
  • 1칸 또는 2칸 전의 최소값을 구한 뒤 cost[i]를 더해줌
  • 1칸 또는 2칸 올라가면 정상에 도착할 수 있으므로 Math.min(dp[n-1], dp[n-2])
profile
백엔드 개발자

0개의 댓글