题目描述:

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:

  1. cost will have a length in the range [2, 1000].
  2. 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(动态规划)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

  4. [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 ...

  5. Leetcode 746. Min Cost Climbing Stairs

    思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...

  6. LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)

    题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...

  7. 【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 ...

  8. 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 ...

  9. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

  10. 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 ...

随机推荐

  1. Qt中限制IP输入的正则表达式:

    这个例子中,是使用QLineEdit加入正则表达式来实现ip地址的输入功能的,不符合规范的数据将不能输入: QRegExp regExpIP("((25[0-5]|2[0-4][0-9]|1 ...

  2. CRITICAL **: Couldn't acquire global lock, snapshots will not be consistent: Access denied

    报错如下:** (mydumper:56288): CRITICAL **: Couldn't acquire global lock, snapshots will not be consisten ...

  3. [Python 多线程] asyncio (十六)

    asyncio 该模块是3.4版本加入的新功能. 先来看一个例子: def a(): for x in range(3): print('a.x', x) def b(): for x in 'abc ...

  4. [转]百度地图API详解之地图坐标系统

    博客原文地址:http://www.jiazhengblog.com/blog/2011/07/02/289/ 我们都知道地球是圆的,电脑显示器是平的,要想让位于球面的形状显示在平面的显示器上就必然需 ...

  5. 【Git】将项目下的.git目录隐藏

    将项目下的.git目录隐藏 在apache配置文件httpd.conf中添加配置: <Directory "${INSTALL_DIR}/www/mypro/.git"> ...

  6. POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others ...

  7. 调试libRTMP代码来分析RTMP协议

    RTMP是Real Time Messaging Protocol(实时消息传输协议)的首字母缩写.该协议基于TCP,是一个协议族,常用在视频直播领域.RTMP协议的默认端口是1935. 学习一个协议 ...

  8. cocos2d-x开发: 如何从项目中分离出接口范例

    cocos2d-x开发,包括核心模块接口开发和脚本部分的业务逻辑实现.从上层应用需求开始说,脚本在做业务逻辑实现的时候, 很多时候都需要依赖底层的接口功能,但是不是所有的人都可以游刃有余的去明白该怎么 ...

  9. Linux Shell常用技巧(九)

    十九.  和系统运行进程相关的Shell命令:       1.  进程监控命令(ps):    要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而ps命令就是最基本同时 ...

  10. Java中的集合框架-Collections和Arrays

    上一篇<Java中的集合框架-Map>把集合框架中的键值对容器Map中常用的知识记录了一下,本节记录一下集合框架的两个工具类Collections和Arrays 一,Collections ...