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 the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.
Example 1:
Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
Example 2:
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
由于一次可以走2步,数组的最后一个数肯定是可以跳过不用走的。走2步的埋伏,第一次见。
[思维问题]:
以为爬楼梯是坐标型:746:第0位拿来初始化的爬楼梯 符合序列型
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
新变量要声明啊,别养成一上来就写的习惯啊
[总结]:
由于一次可以走2步,数组的最后一个数肯定是可以跳过不用走的。走2步的埋伏,第一次见。
“第0位”+初始化 = 序列型
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int minCostClimbingStairs(int[] cost) {
//state
int[] f = new int[cost.length + 1];
//ini
f[0] = cost[0];
f[1] = cost[1];
//function
for (int i = 2; i <= cost.length; i++) {
int costV = (i == cost.length) ? 0 : cost[i];
f[i] = Math.min(f[i - 1] + costV, f[i - 2] + costV);
}
//answer
return f[cost.length];
}
}
746. Min Cost Climbing Stairs 最不费力的加权爬楼梯的更多相关文章
- 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 ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 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 ...
- 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 ...
- [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 ...
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【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 ...
随机推荐
- vue-router做路由拦截时陷入死循环
今天分享一下使用vue-router做路由拦截时遇到的坑. 需要提前了解的api 1:router.beforeEach( to , from ,next) ; to: Route: 即将要进入的目标 ...
- (转)Android DiskLruCache完全解析,硬盘缓存的最佳方案
摘自:http://blog.csdn.net/guolin_blog/article/details/28863651 转载请注明出处: http://blog.csdn.net/guolin_bl ...
- 11.Python使用Scrapy爬虫小Demo(新手入门)
1.前提:已安装好scrapy,且已新建好项目,编写小Demo去获取美剧天堂的电影标题名 2.在项目中创建一个python文件 3.代码如下所示: import scrapy class movies ...
- outlook2013插件 VSTO开发与部署
一.背景 最近因为项目需要对outlook开发一个插件,功能是将outlook的邮件作导出功能,需要使用VSTO开发一个插件将邮件进行导出的操作.于是,开始学习VSTO outlook的开发了,折腾了 ...
- Unit07: 状态管理-Session
Unit07: 状态管理-Session web package web; import java.io.IOException; import java.io.PrintWriter; import ...
- 手动下载阿里云Nexus上的Jar包
手动下载阿里云Nexus上的Jar包 1.1 在任意目录下创建一个文件夹,创建一个pom.xml文件,一个bat批处理脚本,如图: 1.2 DownLoad.bat文件中的内容: call mvn - ...
- Erlang ets -- something about cache
都说用ets 写一个cache 太简单, 那就简单的搞一个吧, 具体代码就不贴了, 就说说简要的需求和怎么做(说设计有点虚的慌). 需求场景 >> 查询系统,对于主存储而言,一次写入多次查 ...
- GOF23设计模式之装饰模式(decorator)
一.装饰模式概述 (1)动态的为一个对象增加新的功能. (2)装饰模式是一种用于代替继承的技术,无需通过继承增加子类就能扩展对象的新功能. 使用对象的关联关系代替继承关系,更加灵活,同时避免类型体 ...
- 杂项:Hadoop
ylbtech-杂项:Hadoop Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.Ha ...
- 022:SQL优化--JOIN算法
目录 一. SQL优化--JOIN算法 1.1. JOIN 写法对比 2. JOIN的成本 3. JOIN算法 3.1. simple nested loop join 3.2. index nest ...