题目翻译

有一个楼梯,第i阶用cost[i](非负)表示成本。现在你需要支付这些成本,可以一次走两阶也可以走一阶。 问从地面或者第一阶出发,怎么走成本最小。

测试样例

Input: cost = [10, 15, 20]
Output: 15
Explanation: 从第一阶出发,一次走两步 Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: 从地面出发,走两步,走两步,走两步,走一步,走两步,走一步。

详细分析

现在用step[i]表示走到第i阶的成本,要求step[i],我们只需在"到前一阶的成本+当前阶成本"和"到前两阶的成本+前两阶成本"取最小即可。一图胜千言:

因为step[0]和step[1]都可以作为开始出发地,所以成本都为0。注意一下爬两阶只需要那两阶的第一个成本作为总成本不需要两阶成本相加。所以

step[2] = min{step[1]+cost[1],step[0]+cost[0]} = min{10,15}=10

step[3] = min{step[2]+cost[2],step[1]+cost[1]} = min{30,15} = 15

代码实现

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
if(cost.size()==){
return ;
}
if(cost.size()==){
return cost[];
}
if(cost.size()==){
return std::min(cost[],cost[]);
}
int step[];
step[] = ;
step[] = ;
for(int i=;i<=cost.size();i++){
step[i] = std::min(step[i-]+cost[i-],step[i-]+cost[i-]);
}
return step[cost.size()];
}
};

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 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

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

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

  5. Leetcode 746. Min Cost Climbing Stairs

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

  6. 【Leetcode_easy】746. Min Cost Climbing Stairs

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

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

  8. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  9. 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯

    [抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

随机推荐

  1. Linux 对mysql远程授权连接操作 和 查看mysql数据库和表 基本命令

    Linux 对mysql远程连接的授权操作 首先linux连接mysql数据库 授权: grant all on *.* to ' with grant option; //允许账户root从任何主机 ...

  2. LAMP 3.5 mysql备份与恢复

    备份库的命令 mysqldump -uroot -pwangshaojun discuz > /data/discuz.sql 指定用户密码,重定向到某文件 恢复 mysql -uroot -p ...

  3. TextView 点击拨打电话

    点击TextView,拨打电话 TextView属性:android:autoLink="phone" 需要配置文件中的属性 <uses-permission android ...

  4. 关于FILL_PARENTE和match_parent布局属性

    在观看早期的代码的时候,经常会看到FILL_PARENT属性,但是新的代码中却有了MATCH_PARENT 那么,两者有何区别呢? 答案是,没有,只是换了个名字而已,均为-1

  5. Select2下拉选项库 部分积累

    用了这么久的Select2插件,也该写篇文章总结总结. 在我的印象里Select2有2个版本,最新版本有一些新的特性,并且更新了一下方法参数,比最初版本要好看一些,本文针对新版本. 官网:http:/ ...

  6. day70-oracle 12-Java调用存储过程和存储函数

    我们现在调用的是存储过程和存储函数.用CallableSatement调用存储函数和存储过程. RDBMS:关系数据库.使用标准方式调用存储过程.也就是说:在mysql中调用和在oracle中调用的写 ...

  7. Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练

    # coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...

  8. HBase 协处理器统计行数

    环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...

  9. NSButton添加事件

    -(void)addButton { NSButton* pushButton = [[NSButton alloc] initWithFrame: NSMakeRect(, , , )]; push ...

  10. WOJ 41 约数统计

    只会写60分算法QuQ 考虑到一个数$x$大于$\sqrt{x}$的质因数最多只有一个,我们可以筛出小于$\sqrt{r}$范围内的所有质因数然后直接用这些取分解质因数. 最后扫一遍发现还没有分解完的 ...