problem

746. Min Cost Climbing Stairs

题意:

solution1:动态规划;

定义一个一维的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])

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

solution2:

要爬当前的台阶,肯定需要加上当前的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)//errr..
{
dp[i] = cost[i] + min(dp[i-], dp[i-]);
}
return min(dp[n-], dp[n-]);
}
};

参考

1. Leetcode_easy_746. Min Cost Climbing Stairs;

2. Grandyang;

【Leetcode_easy】746. Min Cost Climbing Stairs的更多相关文章

  1. 【easy】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 t ...

  2. 【Leetcode】746. Min Cost Climbing Stairs

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

  3. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

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

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

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

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

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

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

  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. pycharm连接不上mysql数据库的解决办法

    问题描述 环境:ubuntu18.04,mysql5.7 今天在ubuntu下使用pycharm连接mysql,发现连接不上 这不是缺少驱动吗?下载之! 下好之后点进去 连接 点击test conne ...

  2. The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners

    链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...

  3. Python 装饰器实现单列模式

    # 使用装饰器实现单列模式 def singleton(cls): # 用来存在实例的字典 singleton_instance = {} def wrapper(*args, **kwargs): ...

  4. guava字符串工具--------Joiner 根据给定的分隔符把字符串连接到一起

    public class JoinerTest { public static void main(String args[]){ //1.将list字符串集合,以,形式转为字符串 List<S ...

  5. JsonObject常用转换

    我们在平时的开发中,com.alibaba.fastjson.JSONObject是经常会用到的JSON工具包,同样它的转换方法也会经常被我们使用,包括对象转成JSON串,JSON串转成java对象等 ...

  6. PHP基础语法之 位运算

    写了几年PHP的人都好奇说,没有用过位运算符.所以,此处你看二进制看的头晕,就去T¥M¥D吧. 位运算符基本不用,我们也将这个知识设置为了解级别.位运算符的知识点,你不想学习也可以.等以后用到位运算的 ...

  7. php利用webuploader实现超大文件分片上传、断点续传

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  8. Linux操作系统常用命令合集——第六篇-压缩和归档操作(16个命令)

    1.gzip [命令作用] 压缩和解压缩文件 gzip/guzip/zcat zcat:不显式展开的前提下查看文本文件内容 zdiff/zgrep/zless/zmore [命令语法]  gzip   ...

  9. 携程移动端案列(flex布局、背景图缩放,文字阴影)

    效果图如下: <body> <div class="nav"> <div class="row"> <div clas ...

  10. luogu 2152

    SuperGcd 二进制算法 1. A = B, Gcd(A, B) = A; 2. A,B为偶数,  Gcd(A, B) = 2 * Gcd(A / 2, B / 2); 3. A 为偶数, B 为 ...