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. MySQL 视图 触发器 事务 存储过程 函数 流程控制 索引与慢查询优化

    视图 1.什么是视图? 视图就是通过查询得到的一张虚拟表,然后保存下来,下次可直接使用 2.为什么要使用视图? 如果要频繁使用一张虚拟表,可以不用重复查询 3.如何使用视图? create view ...

  2. SpringMVC数据格式化

    SpringMVC数据格式化 1. 使用Formatter格式化数据 Converter可以将一种类型转换成另一种类型,是任意Object之间的类型转换. Formatter则只能进行String与任 ...

  3. javascript/Jquery 将字符串转换成变量名

    var a = ['a', 'b', 'c'] var obj = {} for(i = 0; i < a.length; i++){ obj[a[i]] = "abc" + ...

  4. sql server 时间处理函数 datediff() 和getdate()

    一: DATEDIFF() 定义和用法 DATEDIFF() 函数返回两个日期之间的时间. 语法 DATEDIFF(datepart,startdate,enddate) startdate 和 en ...

  5. 轻松掌握mongodb

    mongodb 是一种非关系型的,面向文档的数据库,也是nosql类的产品 memcache,redis等等 与mysql最大的区别:mongodb 使用javascript语言操作,保存是以json ...

  6. 24 | MySQL是怎么保证主备一致的?

    在前面的文章中,我不止一次地和你提到了binlog,大家知道binlog可以用来归档,也可以用来做主备同步,但它的内容是什么样的呢?为什么备库执行了binlog就可以跟主库保持一致了呢?今天我就正式地 ...

  7. 008_Python3 列表

           序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表 ...

  8. 简单python脚本,将jupter notebook的ipynb文件转为pdf(包含中文)

    直接执行的python代码ipynb2pdf.py 主要思路.将ipynb文件转成tex文件,然后使用latex编译成pdf.由于latex默认转换不显示中文,需要向tex文件中添加相关中文包. 依赖 ...

  9. 如何利用shell或者awk二维数组实现9x9乘法表?

    第一种:利用shell for循环来实现. for i in `seq 1 9`do for j in `seq 1 9` do if [ $i -ge $j ] then echo -en &quo ...

  10. Linux下SSH命令使用方法详解(摘自网络)

    备注:检查自己的linux系统中是否已经安装了某一些软件的命令: rpm -qa | grep 软件名  例如 rpm -qa | grep  vsftpd 1.查看SSH客户端版本 有的时候需要确认 ...