Min Cost Climbing Stairs - LeetCode
题目链接
Min Cost Climbing Stairs - LeetCode
注意点
- 注意边界条件
解法
解法一:这道题也是一道dp题。dp[i]表示爬到第i层的最小cost,想要到达第i层只有两种可能性,一个是从第i-2层上直接跳上来,一个是从第i-1层上跳上来。所以可以得到dp[i] = min(dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1])。时间复杂度O(n)。
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size(),i;
vector<int> dp(n+1, 0);
for(i = 2;i <= n;i++)
{
dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
}
return dp[n];
}
};

解法二:换一个思路。我们离开这一层一定要花费cost[i],到达这一层我们还是要从前一层或者前两层的台阶上跳上来,所以得到dp[i] = cost[i] + min(dp[i- 1], dp[i - 2]),最后我们在最后两个dp值中选择一个较小的返回即可。时间复杂度O(n)。
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size(),i;
vector<int> dp(n,0);
dp[0] = cost[0];
dp[1] = cost[1];
for(i = 2;i < n;i++)
{
dp[i] = cost[i] + min(dp[i-1],dp[i-2]);
}
return min(dp[n-1],dp[n-2]);
}
};

小结
- 这道题可以扩展到每次可以走k步,那解法一递推式就变为
dp[i] = min(dp[i - k]+cost[i - k],...,dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1])。解法二递推式变为dp[i] = cost[i] + min(dp[i- 1], dp[i - 2],...,dp[i - k])
Min Cost Climbing Stairs - LeetCode的更多相关文章
- 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 ...
- 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 ...
- 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_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- Min Cost Climbing Stairs [746]
Min Cost Climbing Stairs [746] 题目描述 简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以 ...
- [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 ...
随机推荐
- PHP核心技术——接口
接口: 接口这样描述自己:对于实现我的所有类,看起来都应该像我现在这个样子 接口含义:采用一个特定接口的所有代码都知道对于那个接口会调用什么方法. interface mobile{ public f ...
- 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识
深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...
- 前端_html
目录 HTML介绍 标签说明 常用标签 <!DOCTYPE>标签 <head>内常用标签 <body>内常用标签 特殊字符 其他:各种各样的标签 HTML的规范 H ...
- jQuery控制a标签不可用
$('.disableCss').removeAttr('href');//去掉a标签中的href属性 $('.disableCss').removeAttr('onclick');//去掉a标签中的 ...
- JSON toBean Timestamp To Date 时间戳转日期
时间戳格式的时间从json转为date时 配置: import java.util.Date; import net.sf.ezmorph.object.AbstractObjectMorpher; ...
- 在WPF里实现计算器软件
一.具体代码 类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- 安装AndroidJDK的坑
最近公司要用weex了,先开始搭一下环境,真的都是坑,写下来大家引以为鉴,我踩坑三天的后果. 首先要安装JavaJDK这个过程就不写了都是程序员网上搜索一下很多,注意找论坛上最新的帖子来看,这里有一个 ...
- beat冲刺(4/7)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(4/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 整理博客 ppt模板 接下来的计划 做好机动. ...
- golang type
参考链接 https://blog.csdn.net/tzs919/article/details/53571632 type是golang中非常重要的关键字,常见的就是定义结构体,但是其功能远不止是 ...
- 2016011998+sw
Coding.net原码仓库地址:https://git.coding.net/laolaf/sw.git 目录 1 需求分析 2 功能设计 3 设计实现 4 算法详解 5 测试运行 6 满意代码 1 ...