简单题重拳出击 类似跳台阶 动态规划学习ing
java:(自己瞎写)
class Solution {
public int minCostClimbingStairs(int[] cost) {
int ans = 0;
int pre = cost[0];
int cur = cost[1];
int n = cost.length;
for(int i = 2; i < n; i++){
int tmp = cur;
cur = Math.min(pre, cur) + cost[i];
pre = tmp;
}
return Math.min(pre,cur);
}
}
python3:(学习题解)
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [0] * (n + 1)
for i in range(2, n + 1):
dp[i] = min(dp[i - 1] + cost[i - 1],dp[i - 2] + cost[i - 2])
return dp[n]