import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cost int整型一维数组
* @return int整型
*/
public int minCostClimbingStairs (int[] cost) {
// write code here
int[] costs = new int[cost.length];
costs[0] = cost[0];
costs[1] = cost[1];
for (int i = 2; i < costs.length; i++) {
costs[i] = cost[i] + Math.min(costs[i - 1], costs[i - 2]);
}
return Math.min(costs[costs.length - 1], costs[costs.length - 2]);
}
}
costs[i]为从第i个阶梯往上爬需要的最小花费
costs[0] = cost[0],costs[1] = cost[1]
i > 1时,有以下递推公式
costs[i] = cost[i] + min(costs[i - 1], costs[i - 2]);
最后返回costs数组最后两位的较小一个即可。

京公网安备 11010502036488号