LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
题目标签: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 (使用最小花费爬楼梯)的更多相关文章
- Leetcode746.Min Cost Climbing Stairs使用最小花费爬楼梯
数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需 ...
- 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 ...
- 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 ...
- [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 ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- Leetcode 746. Min Cost Climbing Stairs
思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 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 ...
- 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯
[抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...
随机推荐
- 【转】java即时消息推送
整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp 下载服务端jar文件 Comet4J目前仅支持Tomcat6.7版本,根据您所使用的Tomcat版本下载[comet ...
- Vue 事件相关实例方法---on/emit/off/once
一.初始位置 平常项目中写逻辑,避免不了注册/触发各种事件 今天来研究下 Vue 中,我们平常用到的关于 on/emit/off/once 的实现原理 关于事件的方法,是在 Vue 项目下面文件中的 ...
- APIO2019
device: 用最小公倍数的知识或是画网格模拟转移,神仙们也可以找规律.然后就变成区间覆盖了. 忘记特殊情况了,大众分→Ag #include<iostream> #include< ...
- delphi 给程序加托盘图标
一些程序运行时,会在桌面的右下角显示一个图标(任务栏的右边),这类图标称为 托盘.托盘是一个PNotifyIconDataA类型的结构,要增加托盘图标其实就是对结构PNotifyIconDataA的操 ...
- django_websocket实现简单聊天室
一.安装模块 pip install channels pip install channels-redis 二.代码 #websocket_v1/settings.py INSTALLED_APPS ...
- VC内联汇编,引用程序中的变量
int a=5; //变量a _asm { mov eax,a; //将变量a的值放入寄存器eax add eax,eax; //相当于a=a+a mov a,eax; // ...
- (转)OpenFire源码学习之二十七:Smack源码解析
转:http://blog.csdn.net/huwenfeng_2011/article/details/43484199 Smack Smack是一个用于和XMPP服务器通信的类库,由此可以实现即 ...
- Shell基本正则表达式和扩展正则表达式
BRE和ERE的区别 基本正则表达式(BRE)和扩展正则表达式(ERE)的区别仅仅是元字符(meta charactor)的区别而已. BRE: 只有^$.*[]是元字符 ERE: ^$.[]*+() ...
- unittest框架学习笔记五之参数化
例子一: # coding=utf-8'''created:2018/3/29 author:star project:lianxi canshuhua'''from selenium import ...
- ssh 登陆免 known_hosts 提示
修改配置文件 “~/.ssh/config”,加上这两行,重启服务器: StrictHostKeyChecking no UserKnownHostsFile /dev/null