题目翻译

有一个楼梯,第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. 新版本Ubuntu本地提权漏洞复现

    该漏洞在老版本中被修复了,但新的版本还存在漏洞 影响范围:Linux Kernel Version 4.14-4.4,Ubuntu/Debian发行版本 Exp下载地址:http://cyseclab ...

  2. Drools学习笔记4—Consequence/RHS

    Right Hand Side,当LHS所有条件满足才会执行 可以使用LHS部分定义的绑定变量.全局变量.或者直接编写JAVA代码. 提供宏函数操作working memory fact对象,如ins ...

  3. Android 自定义带回调的Dialog 及EditText相关

      import android.app.Activity; import android.content.Context; import android.text.Editable; import ...

  4. AudioFormat

    AudioFormat   用于访问 一系列语音格式和通道配置常量 例如用于AudioTrack 和AudioRecord中 The AudioFormat class is used to acce ...

  5. JVM实用参数(二)参数分类和即时(JIT)编译器诊断

    JVM实用参数(二)参数分类和即时(JIT)编译器诊断 作者: PATRICK PESCHLOW     原文地址    译者:赵峰 校对:许巧辉 在这个系列的第二部分,我来介绍一下HotSpot J ...

  6. NULL、0、nullptr

    C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #defi ...

  7. ROS Learning-001 安装 ROS indigo

    如何在 Ubuntu14.04 上安装 ROS indigo 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本 ...

  8. 杭电acm 1076题

    水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...

  9. UltraISO制作系统ISO镜像

    一.简介 UltraISO是一款功能强大而又方便实用的光盘映像文件制作/编辑/转换工具,它可以直接编辑ISO文件和从ISO中提取文件和目录,也可以从CD-ROM制作光盘映像或者将硬盘上的文件制作成IS ...

  10. 100725B Banal Tickets

    传送门 题目大意 有2*n个位置,这些位置有的已经填上了数,有的还没有(用?表示),现在让你在还没有填上数的填0~9中的任意数,使得前n个数的乘积等于后n个数的乘积,问有多少种方案. 分析 首先这个题 ...