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层 ...
随机推荐
- 012.Nginx负载均衡
一 负载均衡概述 1.1 负载均衡介绍 负载均衡是将负载分摊到多个操作单元上执行,从而提高服务的可用性和响应速度,带给用户更好的体验.对于Web应用,通过负载均衡,可以将一台服务器的工作扩展到多台服务 ...
- scrapyd+gerapy的项目部署
scrapyd+gerapy的项目部署: 简单学习,后续跟进完善 声明: 1)仅作为个人学习,如有冒犯,告知速删! 2)不想误导,如有错误,不吝指教! 环境配置: scrapyd下载: pip ins ...
- DJANGO-天天生鲜项目从0到1-011-订单-订单提交和创建
本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...
- pycharm控制台输出的日志全是红色的字体?
问题:logging在pycharm控制台输出的日志的字体全是红色的,怎么办? 图片描述: 解决办法:设置 -> 搜索“Console” -> 结果:改完立马生效
- 给Django Admin添加验证码和多次登录尝试限制
Django自带的Admin很好用,但是放到生产环境总还差了点什么= = 看看admin的介绍: Django奉行Python的内置电池哲学.它自带了一系列在Web开发中用于解决常见问题或需求的额外的 ...
- 跟老刘学运维day02~新手必须掌握的Linux命令(2)
第2章 Linux命令 1.Shell 计算机硬件:由运算器.控制器.存储器.输入/输出设备等共同组成 Shell:人与硬件的翻译官,人要想使用硬件,需要服务程序 Bash四大好处: (1)通过上下方 ...
- MySQL中change与modify的用法与区别
浅析MySQL中change与modify的区别 MySQL版本 show variables like 'version'; 表结构 desc student; 修改表 例如:修改表studen ...
- Mysql安装使用教程
一:简介 MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理.MySQL是开放源代码的,因此任何人都可以在Genera ...
- c++输出左右对齐设置
#include<iostream> int main(){ using std::cout; cout.setf(std::ios::left); int w = cout.width( ...
- 01_Linux基础篇
学于黑马.传智播客.尚硅谷 感谢 黑马官网 传智播客官网 尚硅谷官网 微信搜索"艺术行者",关注并回复关键词"linux"获取视频和教程资料! b站在线视频 第 ...