Question:

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.

在楼梯上,第i个步骤具有一些非负成本成本[i]赋值(0索引)。

一旦你付出了代价,你可以爬一两步。您需要找到到达楼顶的最小成本,您可以从索引0的步骤开始,也可以从索引1的步骤开始。

Example:

  1. Input: cost = [10, 15, 20]
  2. Output: 15
  3. Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
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].

Solution:

/**
 * @param {number[]} cost
 * @return {number}
 */

//ps:完全看不懂题目什么鬼
var minCostClimbingStairs = function(cost) {
    for(let i = 2; i< cost.length ; i++){
        cost[i]+=Math.min(cost[i-1],cost[i-2])
    }
    return Math.min(cost[cost.length-1],cost[cost.length-2])
};