Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译
有一个楼梯,第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 最小成本爬楼梯 (动态规划)的更多相关文章
- 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 爬楼梯的最小损失
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 (使用最小花费爬楼梯)
题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...
- Leetcode 746. Min Cost Climbing Stairs
思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...
- 【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 ...
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯
[抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...
随机推荐
- 目录扫描工具-Dirsearch
下载项目,并打开 ┌─[root@sch01ar]─[/sch01ar] └──╼ #git clone https://github.com/maurosoria/dirsearch ┌─[root ...
- react核心知识点高度总结
本文系统的将react的语法以最简练的方式列举出来 安装 写在前面 JSX 组件的定义 state 生命周期 方法 条件渲染 列表 表单 组合嵌套 扩展语法 context传递props 错误拦截 r ...
- maven依赖的添加
maven可是个管理jar依赖的好玩意,不用再关心导这个jar包那个jar包,这个jar包是谁家的,和谁有啥关系.有了maven,简简单单就搞定,下面以eclipse为例,在一个springboo ...
- CIA泄露资料分析(黑客工具&技术)—Windows篇
背景 近期,维基解密曝光了一系列据称来自美国中央情报局(CIA)网络攻击活动的秘密文件,代号为“Vault 7”,被泄露文件的第一部分名为“Year Zero”,共有8761个文件,包含7818个网页 ...
- django的render的说明
return render(request,"homesite.html",locals()) homesite.html页面中的所有内容都可以被渲染,不论是标签还是js代码,包括 ...
- 转载-你应该知道的 RPC 原理
在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...
- Acviticy.this 和 getApplicationContext()的区别
用AlertDialog 举例 AlertDialog对象是依赖于一个View的,而View是和一个Activity对应的,在Activity销毁的时候它也就销毁了,不会再存在.Activity.th ...
- IE的haslayout
haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元 ...
- 1 ffmpeg介绍
重点讲解解码的过程.FFmpeg可以进行X264编码.软编码效率是非常低的.即时你编VGA的话它的效率也很低.
- C++数组对象和构造函数
定义数组对象以后,对数组中的对象初始化的方式分为两种: 一种方式是在定义的时候用列表初始化 A a[5] = {A(1),A(2),A(3),A(4),A(5)}; 一种方式是在定义了数组对象以后,再 ...