lc 746 Min Cost Climbing Stairs


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

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

DP Accepted

dp[i]代表从i起跳所需要付出的最小代价,很明显dp[0] = cost[0],且dp1 = cost1,对于i >= 2的情况,dp[i] = min(dp[i-1] + cost[i], dp[i-2] + cost[i]),即跳到i点的那一步要么是一步跳要么是两步跳,取最小值,而这道题的答案很明显就是min(dp[cost.size()-1], dp[cost.size()-2])。

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

LN : leetcode 746 Min Cost Climbing Stairs的更多相关文章

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

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

  3. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

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

    题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...

  5. Leetcode 746. Min Cost Climbing Stairs

    思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...

  6. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

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

  8. [LC] 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 ...

  9. 【Leetcode】746. Min Cost Climbing Stairs

    题目地址: https://leetcode.com/problems/min-cost-climbing-stairs/description/ 解题思路: 官方给出的做法是倒着来,其实正着来也可以 ...

随机推荐

  1. 微信小程序引入外部js 方法

    步骤: 1.首先将外部js放在你指定的文件夹里(这都是废话...) 2.接下来 将该js文件中你要使用的方法给暴露出来 3.在您要使用的js中引入该js 4.使用暴露出来的方法 例子:使用md5加密 ...

  2. javascript查找子节点时,html里的换行可能会被当成节点

    1.直接去HTML里找到该换行的地方去掉换行 2.写一个方法把元素类型为空格而且是文本都删除 function del_ff(elem){ var elem_child = elem.childNod ...

  3. JavaScript实现Map、Reduce和Filter

    1. [代码][JavaScript]代码     <script type="text/javascript">// 函数式编程:// 描述我们要做什么,而不是我们如 ...

  4. cassandra在服务端像leveldb一样进行插入初试成功

    经过研究,决定在 cql3/QueryProcessor.java 里面下手. 这里有两个函数,第一个是 public ResultMessage process(String queryString ...

  5. [ZJOI 2008] 骑士

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1040 [算法] 首先 , 题目中互相讨厌的关系构成了一棵基环森林 用拓扑排序找出环 ...

  6. 洛谷P3195 [HNOI2008]玩具装箱TOY——斜率优化DP

    题目:https://www.luogu.org/problemnew/show/P3195 第一次用斜率优化...其实还是有点云里雾里的: 网上的题解都很详细,我的理解就是通过把式子变形,假定一个最 ...

  7. RDA PQ工具使用 (Adi Analysis)

    PQ工具“ColorAdjustTool.exe”,请注意芯片的选择: RDA512C选择533 RDA8501选择331 RDA8503选择131  工模菜单 COLOR LUT: R/G/B/Y/ ...

  8. 当把链接保存到手机桌面。设置图标 只在safari浏览器中有用

    <link rel="apple-touch-icon" sizes="114x114" href="images/logo.png" ...

  9. 设计模式——模板模式(Template Pattern)

    在读Spring源码的时候,发现Spring代码中运用了大量的模板模式,比如根据文件系统目录加载配置文件(FileSystemXmlApplicationContext),类路径加载配置文件(Clas ...

  10. E20170415-ms

    opaque adj 不透明的 n 不透明 adapter n 配适器