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 ...
随机推荐
- Python内置模块-time && datatime
Python提供两种时间表示方式,一种是时间戳,从1970年1月1日 0时开始.一种是struct_time数组格式,共有9个元素. import time print(time.time()) #返 ...
- flutter 调用摄像头和照片
设置一个按钮调用 打开showCupertinoModalPopup FloatingActionButton(onPressed: (){ _showDialog(context); }, chil ...
- Java线程池ThreadPoolExecutor使用和分析
线程池是可以控制线程创建.释放,并通过某种策略尝试复用线程去执行任务的一种管理框架,从而实现线程资源与任务之间的一种平衡. 以下分析基于 JDK1.7 转自: http://www.cnblogs. ...
- springboot接口:CommandLineRunner
springBoot接口:CommandLineRunner 一.作用: 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Com ...
- delphi基础篇之数据类型
Object Pascal 数据类型 数据类型与定义变量 Object Pascal 语言的最大特点是对数据类型的要求非常严谨.传递给过程或函数的参数值必须与形参的类型一致.在Object ...
- MacOS安装npm全局包的权限问题
MacOS,安装npm全局包提示没有写入权限: npm WARN checkPermissions Missing write access to /usr/local/lib/node_module ...
- 关于版本管理工具SVN
曾经使用过Git,但是目前使用的是相对简单的svn. 刚使用的时候,如果不出意外.会有同学配好环境与权限. 前端只需要将代码下载,修改,更新,与上传. 一 .下载 1.本地新建文件夹,作为本地仓库 命 ...
- python库argparse中type的新奇指定方法
最近在看一些项目的源码,总是能学到好多东西. 关于arparse中type的类型指定 不止可以指定常规类型,还可以加一些自己类型判断,具体用法如下(来源): def str2bool(v): &quo ...
- spring boot Swagger2(version=2.7.0) 注解@ApiImplicitParam的属性dataType值为”自定义泛型“应用
注解: @ApiImplicitParams @ApiImplicitParam name="需注解的API输入参数", value="接收参数的意义描述" ...
- SQLServer2008上的SDE备份和还原
一.备份 右键数据库>任务>备份.选择完整模式,导出为xxx.bak文件即可. 二.还原 1.创建sde用户名,新建同名数据库xxx,并指定sde为xxx的拥有者. 2.在master上创 ...