题目地址:

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. dump文件

    https://blog.csdn.net/icandoit_2014/article/details/78739962 可以看出,此种方法只适用于程序崩溃但没有立即自行退出的情况.倘若程序故障后自行 ...

  2. keepalived vip removed with dhcp renewal【原创】

    最近发现公司云平台服务器的vip有丢失的现象,查看keepalived日志 Jun :: lb1 dhclient: DHCPREQUEST of (xid=0x6deab016) Jun :: lb ...

  3. Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 Tracepoint 是散落在内核源代码中的一些 hook,一旦使能,它们便可以在特定的代码被运行到时被触发,这一特性可以被各种 trace/debug 工具所使用。Perf 就是该特性的用户之一。

    Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/i ...

  4. 关于微信手机端IOS系统中input输入框无法输入的问题

    如果网站不需要阻止用户的选择内容的行为就可以使用如下样式: * { -webkit-user-select: text; -user-select: text;}另一种方式: *: not(input ...

  5. python+opencv抠图并旋转(根据坐标抠图)

    import cv2 import numpy as np def subimage(image, center, theta, width, height): theta *= np.pi / 18 ...

  6. Leetcode: Stream of Characters

    Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data struc ...

  7. Python之Django之views中视图代码重复查询的优化

    Django框架中views视图中如果多个函数都有同样的查询语句,例如: allcategory = Category.objects.all() remen = Article.objects.fi ...

  8. 蒙特卡洛树搜索算法 —— github上的implement的原代码

    首先在网上看到了关于蒙特卡洛搜索算法的介绍,如下: https://www.cnblogs.com/steven-yang/p/5993205.html 并从中发现了一个在GitHub上impleme ...

  9. python-learning-第二季-画图matplotlib

    https://www.bjsxt.com/down/8468.html 绘制方法: 绘制直线: #coding:utf- import matplotlib.pyplot as plt #准备绘制的 ...

  10. 软件定义网络基础---SDN数据平面

    主要介绍SDN架构和转发模型 一:传统网络设备 (一)传统设备控制平面和数据平面 (二)数据平面的任务 数据平面对数据包的处理,主要通过查询由控制平面所生成的转发信息表来完成 (三)传统网络数据平面数 ...