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].
class Solution {
public int minCostClimbingStairs(int[] cost) {
if (cost.length == 1) {
return cost[0];
}
if (cost.length == 2) {
return Math.min(cost[0], cost[1]);
}
int cur = 0, res = 0;
int twoBefore = cost[0], oneBefore = cost[1];
for (int i = 2; i < cost.length; i++) {
cur = Math.min(twoBefore, oneBefore) + cost[i];
twoBefore = oneBefore;
oneBefore = cur;
}
return Math.min(twoBefore, oneBefore);
}
}

[LC] 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. 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯

    [抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

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

  9. [LeetCode&Python] Problem 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 ...

随机推荐

  1. es和数据库关系对比

    es类比传统关系型数据库:   Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -& ...

  2. render_template()的各种用法

    1.可以有很多个参数,第一个一定是模板的名字 2.可以传字典.列表.单个变量等等,还可以传函数,在模板中调用函数 后端函数: from flask import Flask from flask im ...

  3. python中ndarray和matrix

    1. 定义ndarray和matrix from numpy import * a = mat([[1,2],[3,4]]) b = mat([[5,6],[7,8]]) c = array([1,2 ...

  4. Django2.0模型基础——(一)

    Django模型封装python操作数据库的代码,让我们可以更加方便的执行SQL语句.每个创建的app下都会有一个叫models.py的文件,在这个文件下创建的模型类映射于数据库的表名,类属性映射于数 ...

  5. 第 10 章 gdb

    一.参考网址 1.linux c编程一站式学习 二.命令列表 1.图1: 2.图2: 3.图3: 三.重点摘抄 1.断点与观测点的区别 我们知道断点是当程序执行到某一代码行时中断,而观察点是当程序访问 ...

  6. l1 和l2范数的真实意义

    很长时间一直没有明白真实的含义,十一期间补充一下这方面的知识. l0 范数是 ||x||0 = xi (xi不等于0)代表非0数字的个数,[1,2,3,4,5]  非0个数为5,[0,1,2,0,3] ...

  7. 201409-2 画图 Java

    思路: 法1:计算每个矩形的小方块,去掉重复的 法2:二维数组,需要涂色就置flag为1,最后遍历输出,不会有重复计算 import java.util.Scanner; public class M ...

  8. PAT Basic 1017 A除以B (20) [数学问题-⼤整数运算]

    题目 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成⽴. 输⼊格式: 输⼊在1⾏中依次给出A和B,中间以1空格分隔. ...

  9. UML-类图-需要写关联名称吗?

    概念模型:需要写关联名称:类图:不需要写关联名称. 注意,概念模型关联线不需要箭头.

  10. java基础一(2020.1.3)

    今日学习内容: 带命令行参数的Java实例 Java的程序结构 Java的变量与常量 带命令行参数的Java实例: class ArgsDemo{ public static void main(St ...