zoukankan      html  css  js  c++  java
  • 746. Min Cost Climbing Stairs

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

    Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

    可以从0或者1层开始走,每次可以选择走一层或者两层,代价是当前位置的cost

    动态规划dp[i]表示从0或1楼走到第i层的时候,再往上走的最小的代价,转移方程dp[i] = min(dp[i - 1] , dp[i - 2]) + cost[i]

    最后答案就是最后一层和倒数第二层,再往上走的最小代价的最小值。min(dp[n - 1], dp[n - 2])

    class Solution(object):
        def minCostClimbingStairs(self, cost):
            """
            :type cost: List[int]
            :rtype: int
            """
            n = len(cost)
            dp = [0] * (n)
            dp[0] = cost[0]
            dp[1] = cost[1]
            for i in range(2, n, 1):
                dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
            return min(dp[n - 1], dp[n - 2])
  • 相关阅读:
    cavans笔记
    input心得
    杂乱的笔记
    CSS学习目录
    CSS3四个自适应关键字——fill-available、max-content、min-content、fit-content
    闭包
    0..0 小白
    Scrum
    Git与GitHub
    博客1
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13254194.html
Copyright © 2011-2022 走看看