ZhongKuo0228 / study

0 stars 0 forks source link

746. Min Cost Climbing Stairs #116

Open fockspaces opened 11 months ago

fockspaces commented 11 months ago
  1. use dp with initial states 0
  2. dp[i] can choose either dp[i - 1] + cost[i - 1] or dp[i - 2] + cost[i - 2]
class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        dp = [0] * (len(cost) + 1)
        for i in range(2, len(dp)):
            dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
        return dp[-1]