力扣 746. 使用最小花费爬楼梯(dp)

在这里插入图片描述

class Solution {
    
    
    public int minCostClimbingStairs(int[] cost) {
    
    
        if(cost == null || cost.length == 0)
            return 0;
        if(cost.length == 1)
            return cost[0];
        if(cost.length == 2)
            return Math.min(cost[0],cost[1]);
        int[] a = new int[cost.length + 1];
        a[0] = cost[0];
        a[1] = cost[1];
        for(int i = 2;i < a.length;i++){
    
    
            if(i == a.length - 1)
                return Math.min(a[i-1],a[i-2]);
            else
                a[i] = Math.min(a[i-1]+cost[i],a[i-2]+cost[i]);
        }
        return a[a.length-1];
    }
}

猜你喜欢

转载自blog.csdn.net/m0_45311187/article/details/110802397