力扣链接:746.使用最小花费爬楼梯
难度:⭐
解题关键词:动态规划
、斐波那契数列
解题思路:使用一个 dp 数组保存到达第 N 个台阶需要的最小花费。dp[n] = Math.min(dp[n-2]+cost[n-2], dp[n-1]+cost[n-1])
typescript
function minCostClimbingStairs(cost: number[]): number {
const length = cost.length;
const dp = new Array(length + 1);
dp[0] = dp[1] = 0;
for (let i = 2; i <= length; i++) {
dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);
}
return dp[length];
}