题目翻译

有一个楼梯,第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. 数据库学习笔记 (三) python操作数据库

    python 操作MYSQL数据库主要有两种方式: 使用原生模块:pymysql ORM框架:SQLAchemy 一.pymysql 1.1下载安装模块 第一种:cmd下:执行命令下载安装:pip3 ...

  2. javascript——对象的概念——函数 2 (内建函数与类型转换)

    javascript 有许多内建函数,用于各种操作,以下为常用的内建方法. 1.parseInt(object,int):将输入的 int 进制的值 object 转换为 10 进制的数值: obje ...

  3. Python 标准库 -> Pprint 模块 -> 用于打印 Python 数据结构

    使用 pprint 模块 pprint 模块( pretty printer ) 用于打印 Python 数据结构. 当你在命令行下打印特定数据结构时你会发现它很有用(输出格式比较整齐, 便于阅读). ...

  4. 使用pip一次升级所有安装的Python包(太牛了)

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...

  5. java之静态函数和静态变量

    静态变量: 静态变量好似一种成员变量,它的特点是前面有static. 普通变量会有多份,它在每个对象当中都存在,但是静态变量只有一份,它是属于类的. 静态变量的调用方法: 1.类名.变量名 Custo ...

  6. centos7部署func

    Func(Fedora Unitied Network Controller)是红帽公司以Fedora平台构建的统一网络控制器,是为解决集群管理.监控问题而设计开发的系统管理基础框架.它是一个能有效简 ...

  7. contentvalue的探究(结构,用途)

    contentvalue类似HASHMAP,但是KEY只能为STRING 该类用于数据库操作时对数据的封装,可以避免使用SQL语句,为后期创建CONTENTPROVIDER提供便利. 如果没有上述需求 ...

  8. No bean class specified on bean definition 解决方案

    调试Spring项目出现No bean class specified on bean definition异常 但是控制台并没有给出其他相关信息了 这个时候可以在AbstractBeanDefini ...

  9. 02 mybatis环境搭建 【spring + mybatis】

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  10. 电脑安装unity3d有C盘逐渐爆满问题解决方案

    打开unity3d软件,Edit - Preference - GI Cache选中 Custom Cache Location,切换到别的盘,然后点击 Clean Cache清空一次,再查看C盘,存 ...