zoukankan      html  css  js  c++  java
  • 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]
        }
    }
  • 相关阅读:
    如何编写测试用例
    bug的合规描述
    Linux常用命令学习
    测试用列设计
    软件质量管理
    测试的分类
    软件工程模型
    软件测试核心概念
    Thinking in C++ 第十三章 动态对象创建
    python urllib
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12635599.html
Copyright © 2011-2022 走看看