Min Cost Climbing Stairs
You are given an array of integers cost where cost[i] is the cost of taking a step from the ith floor of a staircase. After paying the cost, you can step to either the (i + 1)th floor or the (i + 2)th floor.
You may choose to start at the index 0 or the index 1 floor.
Return the minimum cost to reach the top of the staircase, i.e. just past the last index in cost.
Example 1:
Input: cost = [1,2,3]
Output: 2
Explanation: We can start at index = 1 and pay the cost of cost[1] = 2 and take two steps to reach the top. The total cost is 2.
Example 2:
Input: cost = [1,2,1,2,1,1,1]
Output: 4
Explanation: Start at index = 0.
Pay the cost of cost[0] = 1 and take two steps to reach index = 2.
Pay the cost of cost[2] = 1 and take two steps to reach index = 4.
Pay the cost of cost[4] = 1 and take two steps to reach index = 6.
Pay the cost of cost[6] = 1 and take one step to reach the top.
The total cost is 4.
Constraints:
2 <= cost.length <= 100
0 <= cost[i] <= 100
Solution
A classic dp problem. Let dp[i]dp[i]dp[i] represents the optimal cost spent to climb to stair iii, the transfer function is
dp[i]=min(dp[j − 1]+cost[j − 1],dp[j − 2]+cost[j − 2])
dp[i] = \min(dp[j\!-\!1]+cost[j\!-\!1], dp[j\!-\!2]+cost[j\!-\!2])
dp[i]=min(dp[j−1]+cost[j−1],dp[j−2]+cost[j−2])
Notice that we should initialize a list with cost.length+1cost.length+1cost.length+1 length, where the last value represents the optimal cost we spent to stand on the top.
Code
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]