-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
动态规划This issue or pull request already existsThis issue or pull request already exists完成 ✅待复习第一次📜简单详解Something isn't workingSomething isn't working
Description
题目
数组的每个索引作为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 costi。
每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。
您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。
示例
示例 1:
输入: cost = [10, 15, 20]
输出: 15
解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15。
示例 2:
输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
输出: 6
解释: 最低花费方式是从cost[0]开始,逐个经过那些1,跳过cost[3],一共花费6。
解题
解题方法
/**
* @param {number[]} cost
* @return {number}
*/
var minCostClimbingStairs = function(cost) {
let one = cost[0],two = cost[1],res = 0
for(let i = 2; i <= cost.length; i++) {
res = Math.min(one, two) + (cost[i] || 0)
one = two
two = res
}
return res
};相关题目
Metadata
Metadata
Assignees
Labels
动态规划This issue or pull request already existsThis issue or pull request already exists完成 ✅待复习第一次📜简单详解Something isn't workingSomething isn't working