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 ...
随机推荐
- Mysql学习---使用Python执行存储过程
使用Python执行存储过程 使用Python执行存储过程[2部分]: 1.执行存储过程,获取存储过程的结果集 2.将返回值设置给了 @_存储过程名_序号 = #!/usr/bin/env pyt ...
- January 02 2017 Week 1st Monday
A day is a miniature of eternity. 一天是永恒的缩影. My life is short, but I can make something eternal. What ...
- 36、XmlReader与 XMLWriter(抽象类)
一.概述 XMLReader为抽象类,其派生类有:XmlDictionaryReader.XmlNodeReader.XmlTextReader(与IO命名空间中的TextReader对象一起使用). ...
- kali_metasploit问题
出现类似提示: Failed to connect to the database: could not connect to server: Connection refused Is the ...
- yum安装工具的理解
在安装gtk+编译环境的过程中,你会发现,RPM软件包之间的依赖关系非常复杂.在实际管理过程中,这种依赖关系可能会更加复杂.因此非常有必要寻找一种自动化安装工具,让安装工具自己处理这些关系复杂的依赖关 ...
- window下安装好postgreSQL 9.3用cmd命令进入数据库(搞的我这个菜鸟只剩半条命)
linux下基本没什么问题,但在window操作系统下比较麻烦. 需要添加环境变量path路径:C:\Program Files (x86)\PostgreSQL\9.3\bin 添加postgres ...
- JavaScript设计模式导学
如何成为一名合格的工程师? 作为一名合格的工程师,不仅需要懂代码,还要懂设计,一名合格工程师的必备条件: 前端开发有一定的设计能力,一般三年开发经验的同学,面试必须考设计能力 成为项目技术负责人,设计 ...
- tree视图显示的记录数量
在act_window中,定义limit字段,可以指定打开的tree视图的记录数量. limit:列表视图中每个页面的记录数.
- CentOS查看卸载openjdk
1.查看openjdk版本 java -versionjava version "1.7.0_51" OpenJDK Runtime Environment (rhel-2.4.5 ...
- DG不同步,MRP0进程打不开
问题描述:主库备库之前正常连接,但是昨天磁盘空间满了之后,由于不知什么原因将备库重做日志删了,今天早上发现DG不同步的报警. 当时思路如下:1.通过select thread#,low_sequenc ...