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. .net 4.0 以下HttpWebRequest Header 修改hosts方法

    .net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录 public class CusteredHeaderCollection : WebHeaderCol ...

  2. Natas28 Writeup(ECB分组密码攻击)

    Natas28: 页面显示这是一个笑话库,可以查找提交字符串所在的笑话内容并随机返回. 初步探索 burp抓包发现,流程是post表单提交一个明文后返回一个重定向,然后get请求一个加密参数返回查询结 ...

  3. Recover刷机简介

    Recovery Recovery是一种可以对安卓手机内部的数据文件进行修改的模式,类似电脑的PE.不同的recovery有不同的功能.使用recovery可以说是刷机(卡刷)的基础,想要比较顺畅的刷 ...

  4. 大多数人不知道的表格其他写法的onmouseover效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. 手把手教你用java实现syslog消息的收发,学不会你打我喽!

    大家好,我是道哥,专注于后端java开发,喜欢写作和分享.如果觉得文章对你有用,那就点个赞呗!如果能转发那是对道哥最大的支持! syslog的定义 见文知义,syslog,从英文名字上可以看出是指系统 ...

  6. [快速幂]Codeforces Round #576 (Div. 2)-C. MP3

    C. MP3 time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  7. 【开源】使用Angular9和TypeScript开发RPG游戏(补充了Buffer技能)

    RPG系统构造 通过对于斗罗大陆小说的游戏化过程,熟悉Angular的结构以及使用TypeScript的面向对象开发方法. Github项目源代码地址 RPG系统构造 ver0.02 2020/03/ ...

  8. PHP7内核(一):发展史

    PHP1 1994年,一位名叫Rasmus lerdorf的兄台为了在网上展示自己的履历和网页流量的统计,用Perl开发了一套脚本,后来因与日俱增的需求无法得到满足,lerdorf便使用c语言进行了重 ...

  9. Inception系列理解

    博客:博客园 | CSDN | blog 写在前面 Inception 家族成员:Inception-V1(GoogLeNet).BN-Inception.Inception-V2.Inception ...

  10. Springcloud config + zuul 搭建动态网关

    1,实现的效果,就是zuul 网关的配置路由实现负载均衡,zuul 的配置文件放在springcloud config 上 2,需要的服务如下: 3,其实就是配置下springcloud-zuul 的 ...