问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11

    746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...

  2. [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 ...

  3. Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)

    Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...

  4. Java实现 LeetCode 746 使用最小花费爬楼梯(递推)

    746. 使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 costi. 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶 ...

  5. 【LeetCode】746. 使用最小花费爬楼梯

    使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...

  6. leetcode 746. 使用最小花费爬楼梯

    题目: 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯 ...

  7. 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 ...

  8. 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 ...

  9. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

随机推荐

  1. vue : 自定义脚手架提示

    做项目做烦了就想找点乐子. 比如,我们可以自定义脚手架提示.  webpack.dev.conf.js  54-78 行 module.exports = new Promise((resolve, ...

  2. fastjson将json字符串转化为java对象

    目录 一.导入一个fastjson的jar包 二.Json字符串格式 三.根据json的格式创建Java类 四.给java类的所有属性添加setter方法 五.转换为java对象 一.导入一个fast ...

  3. 02 安装net-tools工具

    01 登录虚拟机,没错,还是那个熟悉的黑窗口 02 输入用户名密码(我还是习惯使用root用户,因为,它可以为所欲为) 小知识:注意红色框内的符号: 一般用户为限制用户,符号为:$ 超级用户,为无限制 ...

  4. C++语法小记---重载逻辑操作符

    重载逻辑操作符 不建议重载逻辑操作符 原因:无法实现逻辑操作符的短路功能(即:不需要计算完全部表达式就可以得出结果) 逻辑操作符:|| && 操作符重载本质上是函数调用,而进行函数调用 ...

  5. Nginx使用SSL模块配置https

    背景 开发微信小程序,需要https域名,因此使用Nginx的SSL模块配置https 步骤 一.去域名管理商(如腾讯云.阿里云等)申请CA证书 二.在Nginx中配置,一般情况下域名管理商会提供配置 ...

  6. Git日常操作指南

    git status git add . git commit -m "注释" git stash # 每次 push 前 git pull --rebase // 如果有冲突,解 ...

  7. 【laravel】基于jwt实现用户认证

    安装及基础配置 使用 composer 安装 # 建议使用1.0以上版本 composer require tymon/jwt-auth .*@rc 进行一些配置 有些文档会说要添加 Tymon\JW ...

  8. 用友U8API 8.9-15.0接口开发前提,选好开发方式

    在用友接口开发这条路上,走走停停过了好几年.对于如何选择哪种方式,目前总结几点, 对于开发,目前可以实现的有三种方式       一.是通过用友官方提供的(EAI/API)接口     这种方式的优点 ...

  9. SpringSecurity匹配规则介绍

    SpringSecurity匹配规则一 URL匹配 requestMatchers() 配置一个request Mather数组,参数为RequestMatcher 对象,其match 规则自定义,需 ...

  10. 进度条函数 -------ajax初试

    做一个显示任务完成情况的进度条: <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...