题解 | #最小花费爬楼梯#

最小花费爬楼梯

https://www.nowcoder.com/practice/6fe0302a058a4e4a834ee44af88435c7

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param cost int整型一维数组 
# @return int整型
#
class Solution:
    def minCostClimbingStairs(self , cost: List[int]) -> int:
        # write code here
        if len(cost)==2:
            return min(cost[0],cost[1])
        if len(cost)==1:
            return cost[0]
        res=[0 for i in range(len(cost))]
        for i in range(2,len(cost)):
            res[i]=min(res[i-2]+cost[i-2],res[i-1]+cost[i-1])
        ans=min(cost[-1]+res[-1],cost[-2]+res[-2])

        return ans

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务