【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索引). ...
随机推荐
- SET NOCOUNT 的用法
SET NOCOUNT 使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息. 语法 SET NOCOUNT { ON | OFF } 注释 当 SET NOCOUNT ...
- pt-table-checksum校验与pt-table-sync修复数据【转】
1:下载工具包 登录网站下载相应的工具包 https://www.percona.com/downloads/percona-toolkit/LATEST/ 2:安装 (1)yum安装: sudo y ...
- Eclipse/STS选择一段文本进行复制变得很卡的解决方案
网上给出的解决方案是: 更改打开代码超链接按键Ctrl为Alt:Window -> Preferences -> General -> Editors -> Text Edit ...
- 《浅谈F5健康检查常用的几种方式》—那些你应该知道的知识(二)
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/sinat_17736151/articl ...
- JEECG MiniDao优劣
Minidao 1.5.1 - 我认为这是一个不严谨的错误 - andotorg的个人空间 - OSCHINAhttps://my.oschina.net/antsdot/blog/1800832 M ...
- 关于[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 的解释
关于[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 的解释 [StructLayout(LayoutKind.Sequentia ...
- php 验证rsa公钥和私钥是否正确
<?php /** * RSA加密 * * @param string $data 待加密数据 * @param string $publicKey 公钥 * @return string|fa ...
- nginx调优(一)
(1).隐藏nginx版本号 隐藏版本号可以有效避免黑客根据nginx版本信息,查找对应漏洞进行攻击. 下载nginx源码包(http://nginx.org/en/download.html)并上传 ...
- Python - Django - 模板语言之自定义过滤器
自定义过滤器的文件: 在 app01 下新建一个 templatetags 的文件夹,然后创建 myfilter.py 文件 这个 templatetags 名字是固定的,myfilter 是自己起的 ...
- NDK开发和NDK双进程守护
https://www.jianshu.com/p/433b2c93c6a7 NDK进程守护 https://blog.csdn.net/k393393/article/details/7895435 ...