Loading...

10-13 leetcode 0746

链接 746. Min Cost Climbing Stairs

题目

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

题解

最简单的一个 dp 题,没啥好说的

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def minCostClimbingStairs(self, cost: list[int]) -> int:
if len(cost) <= 2:
return min(cost)
dp = [1000] * (len(cost) + 1)
dp[0], dp[1] = cost[0], cost[1]
for i in range(2, len(cost) + 1):
dp[i] = (
min(dp[i - 1], dp[i - 2]) + cost[i]
if i < len(cost)
else min(dp[i - 1], dp[i - 2])
)
return dp[-1]

Comment