[抄题]:

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. c/c++程序设计涉及的一些知识点

    c/c++程序设计涉及的一些知识点 c中的printf函数 main(){ int b = 3; int arr[]= {6,7,8,9,10}; int * ptr = arr; *(ptr++) ...

  2. 一次解决spark history server日志不见

    通过cloudera的旧版VM(centos6版本)跑spark,之后,想看一下日志研究一下job,stage以及task,压力很大的发现完全没有日志,180088页面打开后: Event log d ...

  3. error: device not found

    C:\Users\Administrator>adb shell error: device not found    出现上面情况,首先检查设备管理器中,安卓的驱动是否安装OK?   如果驱动 ...

  4. kbuild-(directory)

    -INDEX - this file: info on the kernel build process kbuild.txt - developer information on kbuild kc ...

  5. tensorboard的使用

    # -*- coding: utf-8 -*- """ Created on: 2017/10/29 @author : Shawn function : "& ...

  6. 使用sigaction来取代signal作为信号处理器函数

    早期ISO C提供了像这样的函数来支持自定义信号处理 typedef void (*sighandler)(int); sighandler signal(sighandler func); 但是由于 ...

  7. php通过Mysqli和PDO连接mysql数据详解

    前言 在实际开发中,关于数据库操作类,很少是自己去写,大多是通过一些框架去实现,突然自己去写,还是需要借阅手册之类,于是我觉得有必要去总结一下,php连接mysql的方法,php连接mysql,可以通 ...

  8. FPGA与图像处理

    用FPGA做图像处理最关键的一点优势就是:FPGA能进行实时流水线运算,能达到最高的实时性.因此在一些对实时性要求非常高的应用领域,做图像处理基本就只能用FPGA.例如在一些分选设备中图像处理基本上用 ...

  9. ActiveMQ入门之四--ActiveMQ持久化方式

    消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和 ...

  10. kotlin学习三:初步认识kotlin(第二篇)

    上一章熟悉了kotlin基本的变量和函数声明,并明白了如何调用函数.本章再来看一些其他有用的东西 包括: 1. kotlin代码组织结构 2. when语法 3. 循环迭代语法 4. try表达式 1 ...