[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 ...
随机推荐
- torch_12_BigGAN全文解读
1.摘要: 尽管近来生成图片模型取得了进步,成功生成了高分辨率的图片,但是在复杂的数据集中,样本的多样性仍然是难以捉摸的目标.本文尝试在大规模上训练生成对抗网络,并研究这种规模下的不稳定性.我们发现将 ...
- 【洛谷5438】【XR-2】记忆(数论)
[洛谷5438][XR-2]记忆(数论) 题面 洛谷 题解 很好的一道题目. 我们首先把所有数的每个质因子的出现次数模二,也就是把最大的完全平方因子给除掉.然后剩下部分一样的就可以产生\(1\)的贡献 ...
- 【机器学习笔记】Python机器学习基本语法
本来算法没有那么复杂,但如果因为语法而攻不下就很耽误时间.于是就整理一下,搞python机器学习上都需要些什么基本语法,够用就行,可能会持续更新. Python四大类型 元组tuple,目前还没有感受 ...
- POC挖矿没有前途
最好的工作量证明是能力证明,能力不适合存储.望文生义,能力的本质意义是“能”和“力”(所有的词汇都按照望文生义理解,因为所有不能望文生义的词汇都是不良的已经被前人修正或将来被后人修正)能力是流动的,迁 ...
- Asp.Net SignalR 使用记录
工作上遇到一个推送消息的功能的实现.本着面向百度编程的思想.网上百度了一大堆.主要的实现方式是原生的WebSocket,和SignalR,再次写一个关于Asp.Net SignalR 的demo 这里 ...
- react-custom-scrollbars的使用
react-custom-scrollbars的作用 流畅的本机浏览器滚动 移动设备的本机滚动条 完全可定制 自动隐藏 自动高度 通用(在客户端和服务器上运行) requestAnimationFra ...
- JS初始
简单只和复杂值的区别 1.简单值 简单值表示JS中可用的数据或信息的最底层简单形式. 注:简单之不可被细化. 也就是说,数字是数字,字符是字符,布尔值是true或false,null和undefi ...
- 4. 移动安全渗透测试-(Android逆向基础)
4.1 smali 基础 1.注释 smali中使用#来代表注释一行例如:# const-string v0, "aaa" #这句不会被执行 2.数据类型 V void,只能用于返 ...
- ucoreOS_lab3 实验报告
所有的实验报告将会在 Github 同步更新,更多内容请移步至Github:https://github.com/AngelKitty/review_the_national_post-graduat ...
- jQuery基础的动画里面的回调函数
<style> *{margin:0; padding:0;} #target{ border-radius:10px; background:#eee; } .fade{/*动画起始状态 ...