zoukankan      html  css  js  c++  java
  • 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];
        }
    }
    View Code
  • 相关阅读:
    3n+1问题
    判断x的m次方和y的m次方末尾三位数是否相等
    OpenJudge 计算概论1007:点评赛车
    整数划分问题【转】
    证明:平面内有5个整点,必有两个点连线的中点为整点【本资源整理自网络】
    欧几里德算法的证明
    导出本地和远程SVN项目, Export remote SVN repository
    Centos7的firewalld配置
    ESXi5.5下的Centos7虚机配置静态IP
    Dubbo消费端错误: ClassNotFoundException: org.apache.zookeeper.proto.WatcherEvent
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8555631.html
Copyright © 2011-2022 走看看