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 ...
随机推荐
- Hive开窗函数的理解
1.从一个sql语句开始 select id,sum(price) over(partition by id order by price desc) from books; sum作为聚合函数的时候 ...
- 乘风破浪:LeetCode真题_013_Roman to Integer
乘风破浪:LeetCode真题_013_Roman to Integer 一.前言 上一节我们讨论了如何把阿拉伯数字转换成罗马数字,现在我们需要思考一下如何把罗马数字转换成阿拉伯数字,其实我们仔细观擦 ...
- JavaScript --- Set 集合结构详解
Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用. 1 const set1 = new Set([1, 2, 3, 4, 5]); 2 3 console.log(set1.has ...
- WebSocket消息推送
WebSocket协议是基于TCP的一种新的网络协议,应用层,是TCP/IP协议的子集. 它实现了浏览器与服务器全双工(full-duplex)通信,客户端和服务器都可以向对方主动发送和接收数据.在J ...
- Eclipse 连接真实机器调试
一.手机开启调试模式 二.安装adb.exe 1.确信 \android-sdk-windows\tools\下有 adb.exe AdbWinApi.dll AdbWinUsbApi ...
- BZOJ 1491 社交网络 Floyd 最短路的数目
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1491 题目大意: 见链接 思路: 直接用floyd算法求最短路,同时更新最短路的数目即 ...
- A blog about Core Animation and other iOS graphics framework
A blog about Core Animation and other iOS graphics frameworks. https://www.calayer.com/
- php数组的定义和数组的赋值
1.php执行过程 加载页面 语法检测 执行脚本 $arr=array(1,2,3); 索引数组 $arr=array("name"=>"user1", ...
- javascript-对象搜索算法挑战
对象搜索算法挑战 function where(collection, source) { var arr = []; var status = null; // What's in a name? ...
- [转]谈谈关于MVP模式中V-P交互问题
在差不多两年的时间内,我们项目组几十来号人都扑在一个项目上面.这是一个基于微软SCSF(Smart Client Software Factory)的项目,客户端是墨尔本一家事业单位.前两周,我奉命负 ...