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 ...
随机推荐
- 获取应用程序 或Web页面目录的路径
一.Winform获取本程序的路径 1.获取当前目录 返回最后不带“\”的目录:如D:\Winform\bin\Debug System.Windows.Forms.Application.Start ...
- Debian9+PHP7+MySQL+Apache2配置Thinkphp运行环境LAMP
因工作需要,配置了一台服务器,运行THINKPHP框架程序,记录配置过程如下: 安装net版Debian9,完成后,如下: 1.配置基本的网络 php install net-tools 安装net- ...
- 通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)
package server; import java.io.*; import java.net.*; import java.sql.Time; import java.util.Scanner; ...
- 4、Web Service-Jaxws(Eclipse版本)实现查看天气和手机归属地
1.前提概要 免费的官网:http://www.webxml.com.cn/zh_cn/web_services.aspx 官网提供了各种免费的webservice 我们使用的是:http://ws. ...
- Vue 子组件调用父组件 $emit
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- Windows安装openssl
Windows下有两种方式安装openssl,第一种是采用安装包方式进行安装,第二种是采用编译源码方式进行安装.这里采用第一种方式,简单,直接. windows的openssl安装包的下载地址为:ht ...
- linux的虚拟文件系统VFS
虚拟文件系统(virtual file system),别名虚拟文件系统开关,是linux中的一个软件层,向用户空间提供文件系统操作接口. VFS包含的系统调用包括open(2).stat(2).re ...
- CentOS7安装及相关配置转载
1. 怎样在 CentOS 7.0 上安装和配置 VNC 服务器 https://linux.cn/article-5335-1.html 2. 安装完最小化 RHEL/CentOS 7 后需要做的 ...
- Oracle特有函数 case when decode exists 分页rownum
select * from EMP eselect * from dept dselect * from salgrade s--Oracle特有函数 case whenselect case 2 w ...
- html5手机浏览器启动微信客户端支付实例
html5手机浏览器启动微信客户端支付实例,外部浏览器html5微信支付技术,如何在手机浏览器微信支付,在微信客户端外的移动端网页使用微信支付 首先在微信支付官网https://pay.weixin. ...