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 ...
随机推荐
- springCloud参考资料
官网: http://spring.io/projects/spring-cloud 各组件说明(中文版):https://springcloud.cc/spring-cloud-netflflix. ...
- 分块——cf1207F
这么傻逼的题当时想了那么久 用a数组维护原序列,b[i][j]表示 pos%i=j 的 a[pos]之和 对于每个修改1 x y,先直接修改a[x],然后枚举i=1..700,修改b[i][x%i] ...
- Android 读取<meta-data>元素的数据
在AndroidManifest.xml中,<meta-data>元素可以作为子元素,被包含在<activity>.<application> .<servi ...
- 学习MFC创建界面
原始学习文章地址: http://blog.csdn.net/chenyusiyuan/article/details/4744097 一.创建MFC 首先创建一个MFC对话框应用程序(Dialog- ...
- leetcode-1053. 交换一次的先前排列
题目描述: 给你一个正整数的数组 A(其中的元素不一定完全不同),请你返回可在 一次交换(交换两数字 A[i]和 A[j] 的位置)后得到的.按字典序排列小于 A 的最大可能排列. 如果无法这么操 ...
- 拾遗:nmcli 连接 wifi
... nmcli device wifi list nmcli device wifi connect SSID password PASSWORD ...
- Day 16 : Python 时间模块[time,]datetime[]及第三方模块的下载与安装
在进行python程序开发时,除了可以使用python内置的标准模块外,还右许多第三方模块使用,可以在python官网找到. 在使用第三方模块时,需要下载并安装此模块,然后就可以使用标准模块一样导入并 ...
- java 生成随机数字+字母组合 和字母组合
生成随机数包含数字,字母 /** * 生成随机数当作getItemID * n : 需要的长度 * @return */ private static String getItemID( int n ...
- 高效IO之Java IO体系(一)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 个人觉得可以用“字节流操作类和字符流操作类组成了Java IO体系”来高度概括J ...
- Python匿名函数(lambda函数)
匿名函数 -- 一行函数 lambda -- 关键字 x是普通函数的形参(位置,关键字...)可以不接收参数(x可以不写) :x是普通函数的函数值(只能返回一个数据类型)(:x返回值必须写) 1)此函 ...