The title
Each subscript of the array is a ladder, and the ith ladder corresponds to a non-negative cost cost[I] (the subscript starts at 0).
Every time you climb a ladder, you have to spend your stamina. Once you’ve paid your stamina, you can choose to climb one ladder or two.
Please find the lowest cost to reach the top of the floor. To start, you can choose to start with an element with subscript 0 or 1.
Example 1: Input: cost = [10, 15, 20] Output: 15 Explanation: The lowest cost is to start at cost[1] and then walk two steps to the top of the ladder, which costs 15 in total. Example 2: Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] Output: 6 Explanation: The lowest cost is to start at cost[0] and pass through the ones one by one, skipping cost[3]. The total cost is 6.Copy the code
Tip:
2 <= cost.length <= 1000 0 <= cost[i] <= 999
Their thinking
class Solution: def minCostClimbingStairs(self, cost: [int]) -> int: Dp [0] = cost[0] DP [1] = cost[1] for I in range(2,len(cost)): dp[i] = min(dp[i-1], Dp/I - 2) + cost [I] # print (dp) return min (dp [1], dp [2]) # choice from the first step is the penultimate step across from bottom up if __name__ = = "__main__" : cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] # cost = [10, 15, 20] ret = Solution().minCostClimbingStairs(cost) print(ret)Copy the code