leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

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

solution

top要么是 top - 1 上去的,要么是 top -2 上去的

故动态规划的状态转移方程为:

dp[i] = min{dp[i-1]+cost[i-1], dp[i -2]+cost[i-2]}

可以从第0阶开始,也可以从第1阶开始,故动态规划处理两次即可

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

相关链接

leetcode

leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)的更多相关文章

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

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

随机推荐

  1. nuxt创建项目的步骤

    nuxt创建项目的步骤 1.基本步骤 // 创建package.json依赖管理文件 npm init -y // 在package.json文件中添加运行nuxt的命令,之后npm run dev启 ...

  2. Burpsuite被动扫描流量转发插件:Passive Scan Client

    编译成品:链接: https://pan.baidu.com/s/1E0vsPGgPgB9bXCW-8Yl1gw 提取码: 49eq Passive Scan Client Burpsuite被动扫描 ...

  3. 小米官网轮播图js+css3+html实现

    官网轮播: 我的轮播: 重难点: 1.布局 2.图片和右下角小圆点的同步问题 3.setInterval定时器的使用 4.淡入淡出动画效果 5.左右箭头点击时,图片和小圆点的效果同步 6.另一种轮播思 ...

  4. .net core 依赖注入, autofac 简单使用

    综述 ASP.NET Core  支持依赖注入, 也推荐使用依赖注入. 主要作用是用来降低代码之间的耦合度. 什么是控制反转? 控制反转(Inversion of Control,缩写为IoC),是面 ...

  5. Contest 152

    2019-09-01 20:59:55 总体感受:最近几次参加contest发现自己的水平还是严重的不够,尤其是在处理一些异常情况的时候,遇到TLE,MLE如何有效的进行Debug是需要去锻炼的. 注 ...

  6. WeChat 搭建过程

    [被动回复消息] 1.创建项目(基于MyEclipse + Tomcat 7 编写):wechat 2.导入jar包(用于解析xml):dom4j-1.6.1.jar,xstream-1.3.jar ...

  7. spring boot项目打war包

    1.如果有本地依赖,添加本地依赖到maven <!--lib目录下的jar包--> <dependency> <groupId>com.dm</groupId ...

  8. 用序列到序列和注意模型实现的:Translation with a Sequence to Sequence Network and Attention

    In this project we will be teaching a neural network to translate from French to English. 最后效果: [KEY ...

  9. 3分钟学会简单使用Vim

    Vim是一款运行在命令行里的文字编辑器,它是Linux人员的标配.在Windows环境下也可以有特别的用处,比如创建没有文件名的文件(.gitignore). Vim的功能十分强大,以至于有一些人对它 ...

  10. SimpleITK中术语

    在SimpleITK中,各术语对应如下: Width: 宽度,X轴,矢状面Height: 高度,Y轴,冠状面Depth: 深度, Z轴,横断面 引用自:https://blog.csdn.net/Ji ...