746. Min Cost Climbing Stairs

Posted johnnyzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了746. Min Cost Climbing Stairs相关的知识,希望对你有一定的参考价值。

package LeetCode_746

/**
 * 746. Min Cost Climbing Stairs
 * https://leetcode.com/problems/min-cost-climbing-stairs/description/
 *
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor,
and you can either start from the step with index 0, or the step with index 1.
//1, 100, 1, 1, 1, 100, 1, 1, 100, 1
 * */
class Solution {
    fun minCostClimbingStairs(cost: IntArray): Int {
        if (cost == null || cost.size == 0) {
            return 0
        }
        val dp = IntArray(cost.size+1)
        for (i in 2 .. cost.size) {
            //一个是从第 i-2 层上直接跳上来,一个是从第 i-1 层上跳上来
            //不会再有别的方法,所以 dp[i] 只和前两层有关系
            dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1])
        }
        return dp[dp.size  - 1]
    }
}

 

以上是关于746. Min Cost Climbing Stairs的主要内容,如果未能解决你的问题,请参考以下文章

746. Min Cost Climbing Stairs

LN : leetcode 746 Min Cost Climbing Stairs

[LC] 746. Min Cost Climbing Stairs

746. Min Cost Climbing Stairs(动态规划)

Min Cost Climbing Stairs [746]

leetcode-746-Min Cost Climbing Stairs(动态规划)