LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)
题目
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:
costwill have a length in the range[2, 1000].- Every
cost[i]will be an integer in the range[0, 999].
解法
这是一个动态规划问题,按照以下步骤来解决
1. 分析最优解的性质。并刻画其结构特征。
求最终的最小cost, 因为到最后的cost可以表示为
max(走到最后一层楼梯的最小cost,走到最后二层楼梯的最小cost)
2. 递归的定义最优解
递推定义:
cost = min(上一层的最小cost + 当前层cost, 上两层的最小cost + 当前层cost)
3. 以自底向上或自顶向下的记忆化方式(备忘录法)计算出最优值。
我们需要记录每一层的最优值(最小cost)
定义stairCost数组 stairCost[i]表示走到第i层的最小cost
递推改为:
stairCost[i] = min(stairCost[i-2] + cost[i], stairCost[i-1] + cost[i]);
4 .依据计算最优值时得到的信息,构造问题的最优解。
最优解可以表示为
min(stairCost[cost.size()-2] , stairCost[cost.size()-1]);
代码及注释
解法1:
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
//定义一个数组 表示爬上每一层楼梯 需要的最少cost
vector<int> stairCost(cost.size(), 0);
//第一层楼梯和第二层楼梯的cost就是本身
stairCost[0] = cost[0];
stairCost[1] = cost[1];
//现在循环计算 后面每一层的最小cost
for (int i = 2; i < cost.size(); i++) {
stairCost[i] = min(stairCost[i-2] + cost[i], stairCost[i-1] + cost[i]);
}
//到达楼梯终点的最小cost 就是倒数第一层和第二层的最小cost值
return min(stairCost[cost.size()-2] , stairCost[cost.size()-1]);
}
};
扩展
还有一些非常类似的题目, 有异曲同工之妙
1. LeetCode70——Climbing Stairs
题目如下
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
题目设定都差不多,都是一次只能爬一层或者两次, 问爬到终点的方法共有几种?
当楼梯层数大于2时, 到达当前位置的走法可以表示为:
total_way[n] = total_way [n - 1] + total_way [n - 2]
因为到达楼梯方式有两种, 一种是从前面一层走一步过来, 另外一种是从前面两层走两步过来, 总方法 就是两者之和
其次是目标解 可以表示为最后一层的方法数
代码如下
class Solution {
public:
int climbStairs(int n) {
//特殊数据处理 当n为 1 或 2时, 返回 1 和 2
if (n <= 2) {
return n;
}
//创建数组 用了保存走到当前位置共需要的方法
int * total_way = new int[n];
//初始化条件
//走到第一层一种走法
total_way[0] = 1;
//走到第二层要两种走法
total_way[1] = 2;
//循环递推求解
for (int i = 2; i < n; i++){
//递推式
total_way[i] = total_way[i - 2] + total_way[i - 1];
}
//目的解就是走到最后一块的走法
return total_way[n - 1];
}
};
LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)的更多相关文章
- Leetcode746.Min Cost Climbing Stairs使用最小花费爬楼梯
数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需 ...
- [LeetCode] 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 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- leetcode746 Min Cost Climbing Stairs
""" On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 inde ...
- LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11
746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- 【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 ...
随机推荐
- 题解 CF296B 【Yaroslav and Two Strings】
题目 传送门 题目大意 如果两个只包含数字且长度为 \(n\) 的字符串 \(s\) 和 \(w\) 存在两个数字 \(1≤i,j≤n\),使得 \(s_i<w_i,s_j>w_j\) , ...
- 数据库-SQL查询语言(一)
SQL数据定义 DDL sql的DDL不仅能定义一组关系,还能定义每个关系的信息,包括: 每个关系的模式 每个属性的取值类型 完整性约束 每个关系的维护的索引集合 每个关系的安全性和权限信息 每个关系 ...
- 通过cmd进入指定D盘下的某个文件夹
有时候我们在使用电脑的时候,想使用cmd命令提示符,进入d盘,怎么操作呢,下面来分享一下方法 案例描述:如果进入D盘下的test文件夹(D:\test) 1.win10系统环境下,点击搜索输入[cmd ...
- 拿下Netty这座城,从现在开始!
你好,我是彤哥,技术公号主"彤哥读源码"的运营者. 其实,我刚学习Netty的时候,也是很迷茫的,直到有一天,一个同事收到了阿里的offer,他要去阿里做中台了,临走前他偷偷地告诉 ...
- C#和 JS的闭包
闭包的概念是内层的函数可以引用包含在它外层的函数的变量,即使外层函数的执行已经终止.但该 变量提供的值并非变量创建时的值,而是在父函数范围内的最终值. C#闭包可理解为跨作用域访问函数内变量,那么如何 ...
- Spring Cloud Alibaba教程:Nacos
Nacos是什么 Nacos 致力于帮助您发现.配置和管理微服务,它 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管理. 注册中心 nacos-server 可以 ...
- apache 添加多个站点
虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术.可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口,也可让多个网站拥有不同的域 ...
- java8之Stream流处理
简介 Stream 流处理,首先要澄清的是 java8 中的 Stream 与 I/O 流 InputStream 和 OutputStream 是完全不同的概念. Stream 机制是针对集合迭代器 ...
- SOLID:面向对象设计的前五项原则
S.O.L.I.D是Robert C. Martin提出的前五个面向对象设计(OOD)原则的首字母缩写,他更为人所熟知的名字是Uncle Bob. 将这些原理结合在一起,可使程序员轻松开发易于维护 ...
- 聊聊Django应用的部署和性能的那些事儿
随着工作的深入,我越来越发现Python Web开发中有很多坑,也一直在羡慕AspNetCore和Go等的可执行文件部署和高性能,以及Spring生态的丰富,不过因为工作用了Django,生活还是要继 ...