746. Min Cost Climbing Stairs

Easy
比较straightforward的dp题目

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int[] dp = new int[cost.length + 1];
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = 0;
        for (int i = 3; i < dp.length; i++){
            dp[i] = Math.min(dp[i - 2] + cost[i - 3], dp[i - 1] + cost[i - 2]);
        }
        return Math.min(dp[cost.length] + cost[cost.length - 1], dp[cost.length - 1] + cost[cost.length - 2]);
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/83cedc58e257
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞