[抄题]:

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. 人人都要培养AIQ

    在人工智能时代,要培养自己的AIQ,其中的关键还是区分事实.观点和推理,在平时的工作生活中关注AI的发展,了解AI的优缺点,尝试用AI的优势让自己工作和生活的更好. 如果说IQ是用来测量一个人的智商, ...

  2. U盘永久系统-centos

    U盘永久系统-centos 问题: 服务器centos系统崩溃,重装需要备份其中数据,约4T,实验室有远程存储服务器,然而rescue模式进去后无法挂载远程存储,只好做一个真正的U盘系统解决了. 方案 ...

  3. C++关于变量初始化的琐记

    #include <iostream> using namespace std; class Base{ virtual void func1() { cout<<" ...

  4. Xbox360游戏收藏

    xbox360游戏下载地址 http://dl.3dmgame.com/SoftList_221.html   XBLA游戏总结.http://tieba.baidu.com/p/3174478602 ...

  5. Excel if函数无法正确对比大小

    我想完成以下操作 1.提取A列数字的第7-11位的数字 2.若此数字大于1993 3.则返回20,不然返回0 于是我在B和C列上写了两个函数,分别是 MID(A1,7,4)          IF(B ...

  6. java代码---------再练习ChatAt()的用法

    总结: 没有理解方法的含义.瞎用 package com.mmm; //实现字符串中某个字符出现的次数 public class Mo { public static void main(String ...

  7. HTC8X V版 电信上网方法

    原始V版电信上网设置,转自百度贴吧(http://tieba.baidu.com/p/3224177802). 修改SIM卡设置 修改MIP_MODE 转自贴吧:http://tieba.baidu. ...

  8. webView放弃capture()截图的替代方法

    float scale = webView.getScale(); height = (int) (webView.getContentHeight() * scale + 0.5); bitmap ...

  9. Hadoop MapReduce 初步学习总结

    在Hadoop中一个作业被提交后,其后具体的执行流程要经历Map任务的提交中间结果处理,Reduce任务的分配和执行直至完成这些过程,下面就是MapReduce中作业详细的执行流程图(摘自<Ha ...

  10. @Retention 注解的作用

    注解@Retention可以用来修饰注解,是注解的注解,称为元注解.Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚 ...