【Leetcode】746. Min Cost Climbing Stairs
题目地址:
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的更多相关文章
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- LeetCode算法题-Min Cost Climbing Stairs(Java实现)
这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...
随机推荐
- iview3 版本 升级
Button 废弃 type ghost,原先的 default 样式有改变. Icon 的图标升级至 ionicons 3.0 图标,图标名称有改变. Breadcrumb 废弃 href 属性. ...
- CMU Database Systems - Concurrency Control Theory
并发控制是数据库理论里面最难的课题之一 并发控制首先了解一下事务,transaction 定义如下, 其实transaction关键是,要满足ACID属性, 左边的正式的定义,由于的intuitive ...
- gcov—a Test Coverage Program
gcov—a Test Coverage Program https://coverage.readthedocs.io/en/v4.5.x/cmd.html 覆盖率测试
- JEECG MiniDao优劣
Minidao 1.5.1 - 我认为这是一个不严谨的错误 - andotorg的个人空间 - OSCHINAhttps://my.oschina.net/antsdot/blog/1800832 M ...
- Python的collections之defaultdict的使用及其优势
user_dict = {} users = ["baoshan1", "baoshan2", "baoshan3","baosh ...
- Python3入门(十三)——常用内置模块之时间日期模块datatime
1.日期时间模块——datatime //其他模块例如time.calender等模块暂不展开 (1)获取当前时间:datatime.now(): from datetime import datet ...
- Spring Cloud Hystrix 服务容错保护 5.1
Spring Cloud Hystrix介绍 在微服务架构中,通常会存在多个服务层调用的情况,如果基础服务出现故障可能会发生级联传递,导致整个服务链上的服务不可用为了解决服务级联失败这种问题,在分布式 ...
- Hello log4net——做一个实用好用的log4net的demo(转)
log4net使用指南 (对配置解释比较全面细致,建议做完demo后多看) Log4Net使用详解(周公)——点击打开链接 Log4Net使用详解(续)周公——点击打开链接 点击打开链接 点击打开链 ...
- Java extract amplitude array from recorded wave
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html Extra ...
- WebSocket接收音频,并推送到声卡上
使用信息 import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableM ...