min_cost_climbing_stairs
min_cost_climbing_stairs.py 源码
from typing import List
# 使用最小花费爬楼梯
class Solution:
def minCostClimbingStairs_1(self, cost: List[int]) -> int:
dp = [0 for _ in range(len(cost))]
dp[0], dp[1] = cost[0], cost[1]
for i in range(2, len(cost)):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
return min(dp[-1], dp[-2])
def minCostClimbingStairs_2(self, cost: List[int]) -> int:
prev, back = 0, 0
for i in range(len(cost)):
prev, back = back, min(prev, back) + cost[i]
return min(prev, back)
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦