[抄题]:
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步的埋伏,第一次见。
[思维问题]:
以为爬楼梯是坐标型:单行单列除了字符串,没谁是坐标型。都是序列型:符合多开一位+初始化+返回f[n]的模板
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
新变量要声明啊,别养成一上来就写的习惯啊
[总结]:
由于一次可以走2步,数组的最后一个数肯定是可以跳过不用走的。走2步的埋伏,第一次见。
[复杂度]: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]; } }
原文地址:https://www.cnblogs.com/immiao0319/p/8555631.html