[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 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:
cost
will have a length in the range[2, 1000]
.- Every
cost[i]
will be an integer in the range[0, 999]
.
这道题应该算是之前那道 Climbing Stairs 的拓展,这里不是求步数,而是每个台阶上都有一个 cost,让我们求爬到顶端的最小 cost 是多少。换汤不换药,还是用动态规划 Dynamic Programming 来做。这里定义一个一维的 dp数组,其中 dp[i] 表示爬到第i层的最小 cost,然后来想 dp[i] 如何推导。来思考一下如何才能到第i层呢?是不是只有两种可能性,一个是从第 i-2 层上直接跳上来,一个是从第 i-1 层上跳上来。不会再有别的方法,所以 dp[i] 只和前两层有关系,可以写做如下:
dp[i] = min(dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1])
最后返回最后一个数字dp[n]即可,参见代码如下:
解法一:
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n + );
for (int i = ; i < n + ; ++i) {
dp[i] = min(dp[i- ] + cost[i - ], dp[i - ] + cost[i - ]);
}
return dp.back();
}
};
再来看一种 DP 的解法,跟上面的解法很相近,不同在于 dp 数组长度为n,其中 dp[i] 表示到第 i+1 层的最小 cost,分别初始化 dp[0] 和 dp[1] 为 cost[0] 和 cost[1]。然后从 i=2 处开始遍历,此时更新思路是,要爬当前的台阶,肯定需要加上当前的 cost[i],那么还是要从前一层或者前两层的台阶上跳上来,选择 dp 值小的那个,所以递归式如下:
dp[i] = cost[i] + min(dp[i- 1], dp[i - 2])
最后在最后两个dp值中选择一个较小的返回即可,参见代码如下:
解法二:
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n);
dp[] = cost[]; dp[] = cost[];
for (int i = ; i < n; ++i) {
dp[i] = cost[i] + min(dp[i- ], dp[i - ]);
}
return min(dp[n - ], dp[n - ]);
}
};
我们可以对空间复杂度进行优化,通过前面的分析我们可以发现,当前的 dp 值仅仅依赖前面两个的值,所以不必把整个 dp 数组都记录下来,只需用两个变量a和b来记录前两个值,然后不停的用新得到的值来覆盖它们就好了。初始化a和b均为0,然后遍历 cost 数组,首先将a和b中较小值加上 num 放入临时变量t中,然后把b赋给a,把t赋给b即可,参见代码如下:
解法三:
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int a = , b = ;
for (int num : cost) {
int t = min(a, b) + num;
a = b;
b = t;
}
return min(a, b);
}
};
我们还可以用递归来写,需要优化计算量,即用 HashMap 来保存已经算过了台阶,用的还是 dp 的思想,参见代码如下:
解法四:
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
unordered_map<int, int> memo;
return helper(cost, cost.size(), memo);
}
int helper(vector<int>& cost, int i, unordered_map<int, int>& memo) {
if (memo.count(i)) return memo[i];
if (i <= ) return memo[i] = cost[i];
return memo[i] = (i == cost.size() ? : cost[i]) + min(helper(cost, i - , memo), helper(cost, i - , memo));
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/746
类似题目:
参考资料:
https://leetcode.com/problems/min-cost-climbing-stairs/
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/110109/c-o1-space
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/110111/javascript-and-c-solutions
[LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失的更多相关文章
- [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(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 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- 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 ...
- 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯
[抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...
随机推荐
- 小米笔记本pro 黑苹果系统无法进入系统,频繁重启故障解决记录
问题1:频繁重启,然后clover丢失 表现情况:开机没有选择macos 或windos的界面 解决办法:进入windows使用工具easyefi,直接添加一个clover start boot,选择 ...
- Windows7运行python3,提示缺少api-ms-win-crt-runtime-l1-1.0.dll
一.实验环境 1.Windows7x64_SP1 二.操作步骤 2.1 python官网下载python3.6后,安装.运行,提示如下错误: 2.2 解决方式 去微软官网下载安装:KB2999226补 ...
- Gtksharp编译时提示下载gtk文件问题
Gtksharp编译时提示下载gtk文件问题 1.昨天晚上新建gtksharp项目之后,安装gtksharp之后,编译时无法成功,提示无法下载gtk-3.24.zip 2.记得前几天,另一个项目可以生 ...
- 排序算法Java代码实现(二)—— 冒泡排序
本篇内容: 冒泡排序 冒泡排序 算法思想: 冒泡排序的原理是:从左到右,相邻元素进行比较. 每次比较一轮,就会找到序列中最大的一个或最小的一个.这个数就会从序列的最右边冒出来. 代码实现: /** * ...
- 纯 JS 设置文本框的默认提示
HTML5 中有个新特性叫 placeholder,一般用它来描述输入字段的预期值,适用于 text.search.password 等类型的 input 以及 textarea.示例如下: < ...
- vue笔记(一)
Vue的开发 一丶下载 # 中文下载地址: https://cn.vuejs.org/ # 使用方式: # 1. 单独使用 vue.min.js.文件 # 2. 结合node.js使用集成工具 二丶v ...
- 开发技术--Python核心技术B
开发|Python核心技术B B篇,主要介绍Python的自定义函数,匿名函数,面向对象,模块化. 由于不涉及基础的知识,我会将重难点加以解释. 前言 目前所有的文章思想格式都是:知识+情感. 知识: ...
- python学习3-python views.py的返回值
2.首先要说明一点是,对于HttpRequest对象来说,是Django自己创建的,但是HttpResponse就必须要我们自己创建.注意每个view方法都必须返回一个HttpResponse对象,H ...
- 获取mac地址和扫描端口
获取IP mac地址 arp -a 10.0.0.34 扫描指定IP 1到30000端口 nmap -p1-30000 10.0.0.36
- Shel脚本-初步入门之《02》
Shel脚本-初步入门-什么是 Shell 脚本 2.什么是 Shell脚本 当命令或程序语句不在命令行下执行,而是通过一个程序文件来执行时,改程序就被称为 Shell 脚本.如果在 Shell 脚本 ...