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.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

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:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].

这道题应该算是之前那道 Climbing Stairs 的拓展,这里不是求步数,而是每个台阶上都有一个 cost,让我们求爬到顶端的最小 cost 是多少。换汤不换药,还是用动态规划 Dynamic Programming 来做。这里定义一个一维的 dp数组,其中 dp[i] 表示爬到第i层的最小 cost,然后来想 dp[i] 如何推导。来思考一下如何才能到第i层呢?是不是只有两种可能性,一个是从第 i-2 层上直接跳上来,一个是从第 i-1 层上跳上来。不会再有别的方法,所以 dp[i] 只和前两层有关系,可以写做如下:

dp[i] = min(dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1])

最后返回最后一个数字dp[n]即可,参见代码如下:

解法一:

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n + );
for (int i = ; i < n + ; ++i) {
dp[i] = min(dp[i- ] + cost[i - ], dp[i - ] + cost[i - ]);
}
return dp.back();
}
};

再来看一种 DP 的解法,跟上面的解法很相近,不同在于 dp 数组长度为n,其中 dp[i] 表示到第 i+1 层的最小 cost,分别初始化 dp[0] 和 dp[1] 为 cost[0] 和 cost[1]。然后从 i=2 处开始遍历,此时更新思路是,要爬当前的台阶,肯定需要加上当前的 cost[i],那么还是要从前一层或者前两层的台阶上跳上来,选择 dp 值小的那个,所以递归式如下:

dp[i] = cost[i] + min(dp[i- 1], dp[i - 2])

最后在最后两个dp值中选择一个较小的返回即可,参见代码如下:

解法二:

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n);
dp[] = cost[]; dp[] = cost[];
for (int i = ; i < n; ++i) {
dp[i] = cost[i] + min(dp[i- ], dp[i - ]);
}
return min(dp[n - ], dp[n - ]);
}
};

我们可以对空间复杂度进行优化,通过前面的分析我们可以发现,当前的 dp 值仅仅依赖前面两个的值,所以不必把整个 dp 数组都记录下来,只需用两个变量a和b来记录前两个值,然后不停的用新得到的值来覆盖它们就好了。初始化a和b均为0,然后遍历 cost 数组,首先将a和b中较小值加上 num 放入临时变量t中,然后把b赋给a,把t赋给b即可,参见代码如下:

解法三:

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int a = , b = ;
for (int num : cost) {
int t = min(a, b) + num;
a = b;
b = t;
}
return min(a, b);
}
};

我们还可以用递归来写,需要优化计算量,即用 HashMap 来保存已经算过了台阶,用的还是 dp 的思想,参见代码如下:

解法四:

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
unordered_map<int, int> memo;
return helper(cost, cost.size(), memo);
}
int helper(vector<int>& cost, int i, unordered_map<int, int>& memo) {
if (memo.count(i)) return memo[i];
if (i <= ) return memo[i] = cost[i];
return memo[i] = (i == cost.size() ? : cost[i]) + min(helper(cost, i - , memo), helper(cost, i - , memo));
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/746

类似题目:

Climbing Stairs

参考资料:

https://leetcode.com/problems/min-cost-climbing-stairs/

https://leetcode.com/problems/min-cost-climbing-stairs/discuss/110109/c-o1-space

https://leetcode.com/problems/min-cost-climbing-stairs/discuss/110111/javascript-and-c-solutions

https://leetcode.com/problems/min-cost-climbing-stairs/discuss/144682/3-Lines-Java-Solution-O(1)-space

[LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失的更多相关文章

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

  2. Python3解leetcode Min Cost Climbing Stairs

    问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...

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

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

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

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

  5. [LeetCode] 70. Climbing Stairs 爬楼梯问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  6. [LeetCode] 70. Climbing Stairs 爬楼梯

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. Min Cost Climbing Stairs - LeetCode

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

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

随机推荐

  1. [poj3740]Easy Finding_状态压缩_dfs

    Easy Finding poj-3470 题目大意:给你一个01矩阵,问能否选出一些行,使得这些行所新组成的01矩阵每列中有且只有1个1. 注释:1<=行数<=16,1<=列数&l ...

  2. Cloesest Common Ancestors

    Cloesest Common Ancestors 题目大意:给出一个n个节点的树,m组询问求两点LCA. 注释:n<=900. 想法:这题一看,我去,这不傻题吗?一看读入方式,完了,懵逼了.. ...

  3. attr与prop html与text

  4. 团队开发---”我爱淘“校园二手书店 NABC分析

    本项目特点之一:可预订 N:对于一些抢手的书可以提前预定,避免学生买不到书 A:网上下单,通过手机便捷购物 B:使得订书更加方便快捷 C:二手书摊.网上书店 团队成员:杨广鑫.郭健豪.李明.郑涛

  5. exports

    暴露函数 var bar = require("./bar.js"); var msg = "你好"; var info = "呵呵"; f ...

  6. xcode修改代码目录结构出现clang:error:nosuchfileordirectory解决方法

    需要迁移一个开源工程的一部分内容到自己工程,迁移对方的工程到自己工程之后,因目录结构配置整理需要,对嵌入的工程目录进行了结构改变,编译后出现: clang: error: no such file o ...

  7. [译]RabbitMQ教程C#版 - 工作队列

    先决条件 本教程假定RabbitMQ已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难,可以 ...

  8. JavaScript 动态显示当前时间

    代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  9. api-gateway实践(01)服务网关 - 原型功能

    一.服务注册 1.增加组:LsqGrpA 2.增加版本:LsqVerA 3.增加api:LsqApiA 3.1.基本信息 3.2.前端定义 3.3.后端定义 二.服务上线和服务授权 1.服务上线 2. ...

  10. matlab 对tif数据高程图的处理分析

    temp=z(101:2200,101:2200) 根据图像属性可得此为2300*2300的tif图像,由于需要将其划分为9宫格,所以begin点设置为101,end点设置为2200,temp转化为可 ...