1000243. 使用最小花费爬楼梯
2026/1/12小于 1 分钟约 108 字
1000243. 使用最小花费爬楼梯
难度: Easy
题目描述
English description is not available for the problem. Please switch to Chinese.
解题思路
代码实现
解决方案
java
class Solution {
public int minCostClimbingStairs(int[] cost) {
if(cost.length==2){
return Math.min(cost[0], cost[1]);
}
int n = cost.length;
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 0;
dp[2] = Math.min(cost[0], cost[1]);
for (int i = 3; i <= n; i++) {
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return dp[n];
}
}