본문 바로가기
Problem Solving/LeetCode

[LeetCode] 746. Min Cost Climbing Stairs - Java

by graycode 2026. 10. 8.

• 문제 링크

 

Min Cost Climbing Stairs - LeetCode

Can you solve this real interview question? Min Cost Climbing Stairs - 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 ste

leetcode.com

 

• 풀이 코드

public class Solution {

    public int minCostClimbingStairs(int[] cost) {
        int a = cost[0], b = cost[1];
        for (int i = 2; i < cost.length; i++) {
            int c = cost[i] + Math.min(a, b);
            a = b;
            b = c;
        }

        return Math.min(a, b);
    }

}

댓글