题目地址:

https://leetcode.com/problems/min-cost-climbing-stairs/description/

解题思路:

官方给出的做法是倒着来,其实正着来也可以,无非就是进入某个step有两种方式,退出某一个

step也有两种方式,因此若用dp[i]表示进入第i步并预计从这里退出的最小值,dp[i] = cost[i] + min(dp[i-1],dp[i-2])

最后求退出时最小值,min(dp[i-1],dp[i])

真正写代码时不必用数组记录dp,用f1表示dp[i-2],f2表示dp[i-1].然后用dp[i-1],dp[i]更新f1,和f2

代码:

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int f1 = cost[];
int f2 = cost[];
for (size_t i = ; i < cost.size(); i++)
{
int f3 = cost.at(i) + min(f1, f2);
f1 = f2;
f2 = f3;
} return min(f1,f2);
}
};

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

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

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

  2. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

  3. 【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 ...

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

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

  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. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

随机推荐

  1. Android系统Audio框架介绍【转】

    本文转载自:https://blog.csdn.net/yangwen123/article/details/39502689 音频基础知识声音有哪些重要属性呢? 响度(Loudness)响度就是人类 ...

  2. 5+app uni-app flutter

    5+app uni-app flutter三者区别是什么? - 知乎 https://www.zhihu.com/question/295107584/answer/525442299 崔红保 DCl ...

  3. Oracle 表结构、索引以及分区信息查询

    Oracle 表结构.索引以及分区信息查询 /* 获取表:*/ select table_name from user_tables; --当前用户的表 select table_name from ...

  4. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  5. RestSHarp

    异步扩展: public static class RestClientExtensions { private static Task<T> SelectAsync<T>(t ...

  6. 零基础学Python-第一章 :Python介绍和安装-02.Python的发展历史与版本

    pthhon3.0对开发者带来了麻烦,因为2.0个3.0的程序并不是兼容的. 目前python3.0已经变成了真正的主力 官方版本和发行版 发行版会把常用的科学计算的包也给继承进来.这样就不用考虑包和 ...

  7. ES6深入浅出-7 新版的类(上集)-1.介绍原型

    ES6新出的关键class BE受雇与网景开发了JS 当我们在写一个对象的时候,我们实际上内存的形式表示. obj等于一个空对象,可以直接toString.它为什么可以有toString window ...

  8. docker镜像的导入导出

    docker save > nginx.tar nginx:latest docker load < nginx.tar

  9. PAT 甲级 1065 A+B and C (64bit) (20 分)(溢出判断)*

    1065 A+B and C (64bit) (20 分)   Given three integers A, B and C in [−], you are supposed to tell whe ...

  10. Python中利用原始套接字进行网络编程的示例

    Python中利用原始套接字进行网络编程的示例 在实验中需要自己构造单独的HTTP数据报文,而使用SOCK_STREAM进行发送数据包,需要进行完整的TCP交互. 因此想使用原始套接字进行编程,直接构 ...