码迷,mamicode.com
首页 > 其他好文 > 详细

746. Min Cost Climbing Stairs

时间:2020-04-05 09:48:47      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:math   problem   either   ati   方法   tair   each   Once   ems   

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

标签:math   problem   either   ati   方法   tair   each   Once   ems   

原文地址:https://www.cnblogs.com/johnnyzhao/p/12635599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!