Min Cost Climbing Stairs [746]
Min Cost Climbing Stairs [746]
题目描述
简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以最小的代价达到楼层,也就是跨过所有楼梯
问题解决
穷举法
从第一阶楼梯开始,遍历所有可能的情况,然后选择代价最小的。复杂度会比较高,时间复杂度O(2^n),不太适合
动态规划
逆向解决:从后往前倒退,跳过当前阶楼梯代价最小的情况下需要考虑其面楼梯代价最小的情况,而且每次只能跳一阶或者两阶,也就是说跳过当前fn阶楼梯,需要的代价可以表示为f(n) = cost[n]+ min(f(n+1), f(n+2))(果然编程到最后都是数学问题啊)
这样就找到了解决问题的办法。从最后的楼梯开始,f(n)=cost[n]+min(f(n+1)=0, f(n+2)=0);
f(n-1) = cost[n-1] + min(f(n), f(n+1)),以此类推
代码
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int length = cost.size();
int f1 = 0;
int f2 = 0;
for(int i=length-1; i>=0; i--){
int fn = cost[i] + min(f1, f2);
f2 = f1;
f1 = fn;
}
return min(f1, f2);
}
};
Min Cost Climbing Stairs [746]的更多相关文章
- 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) 11
746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- 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 ...
- 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 ...
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- [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 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
随机推荐
- PHP connection_status() 函数
实例 返回连接状态: <?phpswitch (connection_status()){高佣联盟 www.cgewang.comcase CONNECTION_NORMAL:$txt = 'C ...
- SpringMVC 集成 JWT验证方式
JWT官网: https://jwt.io/ 这里以java的ssm框架为例,集成jwt. 1.pom.xml 导入jwt的包 <!-- jwt --> <dependency> ...
- 谈谈集成测试(integration testing)
对于软件开发来说,软件测试是一个几乎贯穿所有阶段的活动,所以测试的重要性毋庸置疑.不同开发组织如何在不同的产品研发阶段进行测试,也在很大程度上反映了其研发能力和质量控制能力.软件测试有很多类型,包括单 ...
- layui 事件监听触发
1:监听select 改变 <!-- 不用form 用div也可以 --> <form class="layui-form"> <div class= ...
- 2020-04-13:TCP协议本身会导致什么样的安全问题?
福哥答案2020-04-14: 洪泛攻击
- Linux下如何高效切换目录?
Linux 下对于目录的切换,大家肯定会想到一个命令:cd 命令.这个是 Linux 下再基本不过的命令,如果这个命令都不知道的话,赶紧剖腹自尽去吧. cd 命令确实很方便,但如果需要频繁在下面的目录 ...
- Node.js简易服务器,配合type="module" 实现html文件script标签 ES module引入模块
相信大家在测试type="module" 在html文件中直接模块化引入 js时,会出现一个跨域问题. 当我们将<script ></scirpt> 标签t ...
- Android 找不到所标识的资源 java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XX/R$id
报错: java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XXX/R$id; or its supercl ...
- PAT 2-13. 两个有序序列的中位数(25)
题目链接:http://www.patest.cn/contests/ds/2-13 解题思路及代码如下: /* 解题思路: 分别求出序列A 和B 的中位数,设为a 和b,求序列A 和B 的中位数过程 ...
- C++判断是回文串还是镜像串
#include <iostream> #include <string> #include <cstdio> #include <cctype> #p ...