【easy】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 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.
Example 1:
Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
Example 2:
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
Note:
cost
will have a length in the range[2, 1000]
.- Every
cost[i]
will be an integer in the range[0, 999]
.
方法一:正序
/**
* @param cost 每一步所要花费的值
* @return 到达顶部总共需要的值
*/
public int minCostClimbingStairs(int[] cost) {
int length = cost.length + 1;
int[] dp = new int[length];
dp[0] = 0;
dp[1] = 0;
for (int i = 2; i < length; i++) {
dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);
}
return dp[length - 1];
}
方法二:倒序
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int f1 = , f2 = ;
for (int i=cost.size()-; i>= ; i--){
int f0 = cost[i] + min(f1, f2);
f2 = f1;
f1 = f0;
}
return min(f1, f2);
}
};
【easy】746. Min Cost Climbing Stairs 动态规划的更多相关文章
- 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(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 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 ...
- 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 ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- [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 ...
- [LC] 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
思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...
随机推荐
- ora-14400:插入的分区关键字未映射到任何分区
参考:https://blog.csdn.net/rubychen410/article/details/5317553 出现该问题是由于: 1.为表设置了根据时间进行分区(PARTITION),而每 ...
- 证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)
0x01 布尔代数(Boolean algebra) 大名鼎鼎鼎的stephen wolfram在2015年的时候写了一篇介绍George Boole的文章:George Boole: A 200-Y ...
- Linux之判断字符串是否为空
help命令可以查看帮助 help test 正确做法: #!/bin/sh STRING= if [ -z "$STRING" ]; then echo "ST ...
- NodeJs之文件上传
NodeJs之文件上传 一,介绍与需求 1.1,介绍 1,multer模块 multer用于处理文件上传的nodejs中间件,主要跟express框架搭配使用,只支持表单MIME编码为multipar ...
- 设置tomcat开机自启和后台运行
前言:程序登录遇到了问题,重启服务器上的tomcat后程序可以正常的使用,是通过进入bin目录,双击startup.bat运行启动的程序,此时会弹出启动窗口,而且该窗口不能关闭,这个窗口是tomcat ...
- python 第三方库的加载与虚拟机的登录
通过pip来安装python模块(pip方式仅需要联网即可,不需要下载其他文件即可实现扩展库哦的安装.升级和卸载).下载python3.5以上的版本(包括3.5),在我的电脑输入cmd进入命令提示符, ...
- UNION的使用方法 (表与表直接数据和在一起的示例)
SELECT o.CATEGORY CATEGORY,o.KEY_WORK KEY_WORK FROM BO_EU_KEY_WORK wo RIGHT OUTER JOIN BO_EU_WORK_ON ...
- JPA javax.persistence.TransactionRequiredException
直接说一下解决方案 Dao层,一定要是Dao层. 1 增加Transactional,必须要事务! 2 增加Modifying,告诉jpa这是修改! @Transactional @Modifying ...
- MT【325】垂心的向量形式
设$H$为垂心,且$3\overrightarrow{HA}+4\overrightarrow {HB}+5\overrightarrow {HC}=\overrightarrow 0$,则$\cos ...
- 2019余姚培训游记+ZJOJD2划水记
2019余姚培训游记 突然就想写一个... 注意:以下全是胡言乱语的自high,还有很多错别字 Day 0 来的比较早,早上就到了 上午把一本小说看完了,是一个年轻作者的处女作. 我觉得我第一本书一定 ...