746. 使用最小花费爬楼梯

给你一个整数数组 cost ,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。

你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。

请你计算并返回达到楼梯顶部的最低花费。

链接

1
2
3
4
5
6
7
8
9
10
11
function minCostClimbingStairs(cost: number[]): number {
let cur = 0;
let pre = 0;
const costCounter = cost.length;
for(let i = 2; i <= costCounter; i++) {
const next = Math.min(cur + cost[i - 1], pre + cost[i - 2]);
pre = cur;
cur = next;
}
return cur;
};