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 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:
costwill have a length in the range[2, 1000].- Every
cost[i]will be an integer in the range[0, 999].
要完成的函数:
int minCostClimbingStairs(vector<int>& cost)
说明:
1、给定一个vector,里面存放着从各个阶梯“跃起”的代价,每次跃起可以跃起一个台阶,也可以一次性跃起两个台阶。比如[1,100,1,1,1,100,1,1,100,1]第一个元素1,表示从第一个阶梯跃起,到达第二个台阶,或者第三个台阶,的代价都是1。要求一直攀爬楼梯,直到到达vector之外,比如上面给的例子,一共10个台阶,那就要攀爬到第11级,到达vector之外,输出所花费的最小代价。这道题限制攀爬楼梯可以从第一个台阶开始,也可以从第二个台阶开始。
2、上述题目是不是略微嗅到了一点动态规划的味道,不需要找出全部可能的攀爬序列,只需要每次存储一个状态,然后攀爬到下一个台阶存储另一个状态。
不过这道题由于可以跃升一个台阶,也可以一次性跃升两个台阶,所以我们需要存储两个值,比如一个是到达第三个台阶的最小花费,另一个是到达第四个台阶的最小花费,接着我们就可以计算到达第五个台阶和第六个台阶的最小花费,一直这样计算下去。
代码如下:
int minCostClimbingStairs(vector<int>& cost)
{
//先计算从第一个台阶开始攀爬的情况
int i=,j=,s1=cost.size();//i和j表示当前到达的台阶,i在前,j在后
int costi=cost[],costj=cost[],total,totalnew;
//costi表示攀爬到i这一位需要的最小花费,costj同理
while(j!=s1-&&j!=s1-)
{
costi=min(costi+cost[i],costj+cost[j]);//计算到达i+2位的最小花费
i+=;
costj=min(costi+cost[i],costj+cost[j]);//计算到达j+2位的最小花费
j+=;
}
if(j==s1-)//如果j之后还有一个元素
{
costj=costj+cost[j];
costi=costi+cost[i]+cost[i+];
total=min(costi,costj);
}
else
{//如果j已经是最后一位
costi=costi+cost[i];
costj=costj+cost[j];
total=min(costi,costj);
}
//计算从第二个台阶开始攀爬的情况,下述代码同理
i=,j=;
costi=cost[],costj=cost[];
while(j!=s1-&&j!=s1-)
{
costi=min(costi+cost[i],costj+cost[j]);
i+=;
costj=min(costi+cost[i],costj+cost[j]);
j+=;
}
if(j==s1-)
{
costj=costj+cost[j];
costi=costi+cost[i]+cost[i+];
totalnew=min(costi,costj);
}
else
{
costi=costi+cost[i];
costj=costj+cost[j];
totalnew=min(costi,costj);
}
return min(total,totalnew);
}
上述代码实测14ms,beats 49.37% of cpp submissions。
3、改进:
可以从第一个台阶开始,也可以从第二个台阶开始,笔者隐隐觉得这种情况,和我们每一步要处理的两个最小花费,有相似的地方。
于是,我们可以把从第二个台阶开始的情况,也纳入处理的范畴,一起处理,这样可以快很多,代码如下:(附解释,主要修改了costj的初始值)
int minCostClimbingStairs(vector<int>& cost)
{
int i=,j=,s1=cost.size();
int costi=,costj=min(costi+cost[i],cost[]),total;
//costi表示到达i也就是第二个台阶(i从0开始)的最小花费,当前为0
//costj表示到达j也就是第三个台阶(j从0开始)的最小花费,考虑从
//第一个台阶开始攀爬和从第二个台阶开始攀爬的两种情况
while(j!=s1-&&j!=s1-)
{
costi=min(costi+cost[i],costj+cost[j]);
i+=;
costj=min(costi+cost[i],costj+cost[j]);
j+=;
}
if(j==s1-)//把2中未改进的代码简化一下
total=min(costi+cost[i]+cost[i+],costj+cost[j]);
else
total=min(costi+cost[i],costj+cost[j]); return total;
}
上述代码实测12ms,beats 93.11% of cpp submissions。
leetcode-746-Min Cost Climbing Stairs(动态规划)的更多相关文章
- 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 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第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 ...
- Leetcode 746. Min Cost Climbing Stairs
思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...
- LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...
- 【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 t ...
- 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_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 ...
随机推荐
- 沉淀,再出发:VUE的简单理解
沉淀,再出发:VUE的简单理解 一.前言 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过 ...
- C/S架构的性能测试
很多人关心LR在C/S架构上如何实施性能测试,我想根本原因在于两个方面,一是很多时候脚本无法录制,即LR无法成功调用被测的应用程序,二是测试脚本即使录制下来,可读性不强,往往不能运行通过,调试时无从下 ...
- [BZOJ 1924][Sdoi2010]所驼门王的宝藏
1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1285 Solved: 574[Submit][Sta ...
- 团队作业—预则立&&他山之石(人月神教)
1.团队任务 GitHub issues 1.2 团队计划 2.访谈任务 2.1采访对象 采访团队:龙威零式 采访时间:2017.10.23 采访形式:微信群 2.2采访内容 问:你们选题的时候有哪些 ...
- 判断元素(expected_conditions)
判断元素 如何判断一个元素是否存在,如何判断 alert 弹窗出来了,如何判断动态的元素等等一系列的判断,在 selenium 的 expected_conditions 模块收集了一系列的场景判断方 ...
- less使用总结
15年自学了 less ,可是一直没用,就忘记了.后来抱着提高 css 开发速度的目的,又去学习了 less ,学完马上用,效果立竿见影,记得也牢了.刚开始学习前,我们总会问自己一个问题,学习它有什么 ...
- css3—产品列表之鼠标滑过效果
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8" /> <title&g ...
- manbook pro和inode联网
macbook pro可以通过usb以太网转换器来实现有线联网. 1.下载inode 7 2.在终端中输入:sudo /library/StartupItems/iNodeAuthService/iN ...
- UVa 12661 - Funny Car Racing(Dijkstra)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Todolist项目总结 JavaScript+jQuery
Html部分 消息提醒,开始隐藏 内容区 2.1 标题 2.2 表单(输入框.提交按钮) 2.3 清单列表 2.4 任务详情遮罩 2.5 任务详情 3 video引入提示音乐 Css部 ...