C#LeetCode刷题之#746-使用最小花费爬楼梯( Min Cost Climbing Stairs)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4016 访问。
数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。
每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。
您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。
输入: cost = [10, 15, 20]
输出: 15
解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15。
输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
输出: 6
解释: 最低花费方式是从cost[0]开始,逐个经过那些1,跳过cost[3],一共花费6。
注意:
cost 的长度将会在 [2, 1000]。
每一个 cost[i] 将会是一个Integer类型,范围为 [0, 999]。
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.
Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
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].
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4016 访问。
public class Program {
public static void Main(string[] args) {
int[] cost = null;
cost = new int[] { 10, 15, 20 };
var res = MinCostClimbingStairs(cost);
Console.WriteLine(res);
cost = new int[] { 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 };
res = MinCostClimbingStairs2(cost);
Console.WriteLine(res);
Console.ReadKey();
}
private static int MinCostClimbingStairs(int[] cost) {
int length = cost.Length + 1;
int[] step = new int[length];
step[0] = step[1] = 0;
for(int i = 2; i < length; i++) {
//每次选小往上撸
step[i] = Math.Min(step[i - 2] + cost[i - 2], step[i - 1] + cost[i - 1]);
}
return step[length - 1];
}
private static int MinCostClimbingStairs2(int[] cost) {
//减少空间复杂度的解法,原理同上面的
int step1 = cost[0];
int step2 = cost[1];
int min = Math.Min(step1, step2);
for(int i = 2; i < cost.Length; i++) {
step2 = step1;
step1 = min + cost[i];
min = Math.Min(step1, step2);
}
return min;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4016 访问。
15
6
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#746-使用最小花费爬楼梯( Min Cost Climbing Stairs)的更多相关文章
- LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11
746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...
- [Swift]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 ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- Java实现 LeetCode 746 使用最小花费爬楼梯(递推)
746. 使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 costi. 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶 ...
- 【LeetCode】746. 使用最小花费爬楼梯
使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...
- leetcode 746. 使用最小花费爬楼梯
题目: 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯 ...
- 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(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
随机推荐
- project facet java 1.8 is not supported解决办法
Right click on project -> Properties -> Search for Project Facets -> Java (Version)
- linux $* 和$@例子
参见ibm网站示例: https://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html 示例: [ian@pinguino ~]$ ...
- [Qt2D绘图]-02坐标系统&&抗锯齿渲染
本节的内容可以在帮助中通过Coordinate System关键字查看. 或者入门可以看<Qt Creator 快速入门>这本书.强烈推荐入门使用.下面的内容为本书的阅读笔记,喜欢的可以买 ...
- JVM——内存区域:运行时数据区域详解
关注微信公众号:CodingTechWork,一起学习进步. 引言 我们经常会被问到一个问题是Java和C++有何区别?我们除了能回答一个是面向对象.一个是面向过程编程以外,我们还会从底层内存管理 ...
- vue使用elementUI form表单label样式修改
更多关于修改ElementUI样式的方法,可以参考这篇文章 1.删除style标签中的 scoped 属性 <style lang="lang" scoped> ... ...
- 网易邮箱如何使用二次验证码/谷歌身份验证器/两步验证/虚拟MFA?
登陆后点邮箱名——安全设置——开通两步验证,用二次验证码微信小程序绑定即可 具体步骤见链接 网易邮箱如何使用二次验证码/谷歌身份验证器/两步验证/虚拟MFA? 二次验证码小程序于谷歌身份验证器APP ...
- WebApi部署多服务器配置Nginx负载均衡
01PARTCoreWebApi教程本地演示环境 Visual Studio2019 --- Vsersion:16.4.4 + NetCore3.1.2 02PARTNginx快速搭建配置负载均衡 ...
- html头文件设置常用之<meta>设置
也许很多开发人员并没有重视meta标签,我就是其中一个,但是meta标签的功能很强大,下面就来说说meta标签! <meta http-equiv="pragma" cont ...
- netty 使用字典提升短文本的压缩效果
1 问题 术语:压缩率,compression ratio,压缩后的大小/压缩前的大小,越小说明压缩效果越好. 在使用netty的JdkZlibEncoder进行压缩时,发现了一个问题:它对于短文本( ...
- Python延迟初始化(lazy property)
转自:https://blog.csdn.net/azsx02/article/details/77649527 Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建 ...