【LeetCode】746.使用最小花费爬楼梯

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time: 2019/3/18
# @Author: xfLi
# The file...

"""
问题分析:
dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])  # 要考虑楼顶
"""

def minCostClimbingStairs(self, cost: List[int]) -> int:
    n = len(cost)
    dp = [0] * (n + 1)
    if n < 2: return 0
    if n == 0: return min(cost)
    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]

 

你可能感兴趣的:(python,算法)