[抄题]:

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 最不费力的加权爬楼梯的更多相关文章

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

  2. 【Leetcode_easy】746. Min Cost Climbing Stairs

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

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

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

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

  6. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

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

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

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

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

随机推荐

  1. 关于SQL的几道小题详解

    关于SQL的几道小题详解 当我们拿到题目的时候,并不是急于作答,那样会得不偿失的,而是分析思路,采用什么方法,达到什么目的,还要思考有没有简单的方法或者通用的方法等等,这样才会达到以一当十的效果,这样 ...

  2. Linux 之 hugepage 大页内存理论

    HugePages是通过使用大页内存来取代传统的4kb内存页面,使得管理虚拟地址数变少,加快了从虚拟地址到物理地址的映射以及通过摒弃内存页面的换入换出以提高内存的整体性能.尤其是对于8GB以上的内存以 ...

  3. Linux history时间用户ip设置

    Linux history时间用户ip设置        在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再g ...

  4. 优化html标签

    借用Effective之名,开始写Effective系列,总结一些前端的心得. 有些人写页面会走向一个极端,几乎页面所有的标签都用div,究其原因,用div有很多好处,一个是div没有默认样式,不会有 ...

  5. golang里面检测对象是否实现了接口的方法

    写法有点怪异,记一下吧 _, implemented := this.delegate.(IGenTcpServerDelegate) if implemented { this.delegate.G ...

  6. 使用FILE指针读写文件

    http://www.cnblogs.com/Solstice/archive/2011/07/17/2108715.html 看了这篇博客有感而发. 就个人而言我是典型的从C到C++的类型,并且逐渐 ...

  7. css loading 效果

    .loading{ width:160px; height:56px; position: absolute; top:50%; left:50%; line-height:56px; color:# ...

  8. [实践]使用JarJar优雅的发布依赖包

    [实践]使用JarJar优雅的发布依赖包 打包工具: Jar Jar Links是一个Java类库重新打包工具. 可以帮助你将其它用到的java库打包并嵌入到你自己的项目jar包中.这样做的原因有: ...

  9. 一个简单的Java Web项目搭建流程

    今天试图在服务器上搭建一个web服务器,顺便回顾了java web项目的入门,使用Servlet处理HTTP请求,并记录日志等操作.当很久没有做过web项目时,有些东西还是很容易忘记的. Maven配 ...

  10. STL sort

    STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort. ...