746. minCostClimbingStairs
- 题号:746
- 来源:LeetCode
- 难度:简单
- 标签:
dp数组 - 语言:Python
- 解法:1 个
- 作者:lmliheng
- 最近更新:2026-09-29
Python · O(n) 时间 · O(n) 空间 · 更新于 2026-09-29
一维 dp,每步取前一级或前两级的最小花费
python
"""
@difficulty easy
@tags dp,数组
@time O(n)
@space O(n)
@note 一维 dp,每步取前一级或前两级的最小花费
"""
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])
print(dp)
return dp[n]在 GitHub 上查看题目所在目录:lmliheng/algorithm