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. Django --- 与数据库进行交互

    目录 1.静态文件配置 1.什么是静态文件 2.为什么用户在浏览器中输入的网址能够访问到响应的资源?有时候不能访问? 3.如果想要访问静态资源怎么做? 4.手动开设静态文件访问资源 5.关于两个sta ...

  2. codeforces#571Div2 D---Vus the Cossack and Numbers【贪心】

    题目:http://codeforces.com/contest/1186/problem/D 题意:给定一个大小为$n$的浮点序列,这$n$个数的和为0. 现在对这个序列中的每个数,进行向上取整或向 ...

  3. RabbitMQ的安装和管理

    c:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.5.1\sbin>------------------------------- ...

  4. VMware安装Centos7超详细教程

    本篇文章主要介绍了VMware安装Centos7超详细过程(图文),具有一定安装参考价值 在没有运维的情况下,很多时候测试需要自己搭建测试环境,而测试环境又分为QA环境,自动化测试环境,uat环境,以 ...

  5. [bzoj 5143][Ynoi 2018]五彩斑斓的世界

    传送门 Descroption 给了你一个长为n的序列a,有m次操作 1.把区间[l,r]中大于x的数减去x 2.查询区间[l,r]中x的出现次数 Solution 分块 对于每个块,我们都分别维护: ...

  6. 第二次作业社团UML图

    第二次作业 UML图 用例图: 时序图: 申请加入社团 学生可以在页面投递社团加入申请,送交给社团管理员审批 社团活动审批 团委通过社团提交胡活动进行审批 评价活动 社团活动举行完,会有相应的团委和学 ...

  7. Leetcode第三题《Longest Substring Without Repeating Characters》

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  8. 2018-2019-2 网络对抗技术 20165311 Exp 9 Web安全基础

    2018-2019-2 网络对抗技术 20165311 Exp 9 Web安全基础 基础问题回答 实践过程记录 WebGoat安装 SQL注入攻击 1.命令注入(Command Injection) ...

  9. docker-compose EFK查看docker及容器的日志

    上一篇<docker-compose ELK+Filebeat查看docker及容器的日志>已经演示了如何在docker中使用docker-compose创建容器,并将docker中的所有 ...

  10. AT1357 n^p mod m(洛谷)

    题意翻译 求 n^p mod m 的值 输入格式 一行,为整数 n,m,p(注意顺序) 输出格式 一行,为 n^p mod m 的值 数据说明 对于100%的数据 1≤n,m≤10^91≤n,m≤10 ...