题目标签:Dynamic Programming

  题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发。

  因为每次可以选择走一步,还是走两步,这里用 dynamic, 从index 2 (第三格楼梯开始) 计算每一个楼梯,到达需要用的最小cost。

  在每一个楼梯,只需要计算 从前面2格楼梯过来的cost, 和 从前面1格楼梯过来的 cost,哪个小。就选哪个叠加自己的cost。最后 index = len 的地方就是走到top 所用的最小cost。

Java Solution:

Runtime:  1 ms, faster than 99.86%

Memory Usage: 37.9 MB, less than 92.86%

完成日期:08/15/2019

关键点:dynamic programming

class Solution {
public int minCostClimbingStairs(int[] cost) {
int len = cost.length;
int [] mc = new int[len + 1]; // base cases
mc[0] = cost[0];
mc[1] = cost[1]; for(int i = 2; i <= len; i++) {
int c = i == len ? 0 : cost[i];
mc[i] = Math.min(mc[i-1] + c, mc[i-2] + c);
} return mc[len];
}
}

参考资料:LeetCode discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)的更多相关文章

  1. Leetcode746.Min Cost Climbing Stairs使用最小花费爬楼梯

    数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需 ...

  2. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  3. LN : leetcode 746 Min Cost Climbing Stairs

    lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...

  4. [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  5. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

  6. Leetcode 746. Min Cost Climbing Stairs

    思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...

  7. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

  8. 746. Min Cost Climbing Stairs@python

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  9. 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯

    [抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

随机推荐

  1. 30分钟全方位了解阿里云Elasticsearch

    摘要:阿里云Elasticsearch提供100%兼容开源Elasticsearch的功能,以及Security.Machine Learning.Graph.APM等商业功能,致力于数据分析.数据搜 ...

  2. 二分法查找--Python

    二分查找算法,最常规的应用就是在一个有序数组中找特定的数.一般分为四步走: 1. 判定条件为low小于high,low=0, high=size-1 2. mid=(low+high) / 2 3. ...

  3. fiddler抓包工具详解

    转自:http://www.cnblogs.com/yyhh/p/5140852.html Fiddler 抓包工具总结   阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Sta ...

  4. ASP.NET Core学习——5

    日志(Logging)ASP.NET Core内建支持日志,也允许开发人员轻松切换为他们想用的其他日志框架. 通过dependency-injection请求ILoggerFactory或ILogge ...

  5. C++——Lambda表达式

    0.使用场景---只有一两个地方使用的简单操作 独立出来一个函数,但这个函数实现相对简单并且可能在整个项目只使用了一次(即不存在复用的情况),那么这个时候我们就可以考虑使用下lambda表达式了. ? ...

  6. PHP-FPM 远程代码执行漏洞(CVE-2019-11043)复现-含EXP

    搭建容器 安装golang 利用程序 https://github.com/neex/phuip-fpizdam 安装git Cobra包安装 go get -v github.com/spf13/c ...

  7. 【Java多线程系列六】Map实现类

    Map的一些实现类有及其特性 类 线程安全 特性 Hashtable 是 Key不能为null HashMap 否 读写效率最高,但在Java6多线程环境下使用不当可能陷入死循环,进而导致CPU使用率 ...

  8. Keepalived 双主虚拟路由配置实例

    Keepalived 双主虚拟路由配置实例 演示前说明: 2台centos7.2 主机:node-00,node-01 VIP1:10.1.38.19预定node-00占有 VIP2:10.1.38. ...

  9. git stash封存分支 以及关于开发新功能的处理

    有种情况,我们要修复项目的bug时,但别的分支有修改的代码,要修复的bug可能会影响(所有分支共用一个暂存区).可以单独创建一个bug分支,用于修复和提交bug,在修改前可以先stash封存分支修改的 ...

  10. sanic中间件和监听器

    一:中间件 中间件是服务器在请求之前或之后执行的功能,他们可以用来修改修改用户定义处理函数的请求或相应. Sanic提供两种类型的中间件:请求和响应. 两者都是使用@app.middleware装饰器 ...