Min Cost Climbing Stairs
LeetCode 746 · Easy · solution, intuition & step-by-step walkthrough
dp[i] = cost[i] + min(dp[i-1], dp[i-2]); answer = min(dp[n-1], dp[n-2]).
This is how I solved Min Cost Climbing Stairs (LeetCode 746), an easy problem from the 1D / Fibonacci pattern. Below: the intuition, the approach step by step, my accepted Python code and the 3 versions I wrote on the way, animated test cases and a line-by-line debugger.
Intuition
Think of dp[i] as the cheapest total to stand on step i having paid its cost. You can only arrive from i-1 or i-2, so take the cheaper of those and add cost[i]. The top is one step past the end, reachable from either of the last two steps.
Approach
- 1
State: dfs(i) / dp[i] = minimum cost paid to stand on step i.
- 2
Base cases: steps 0 and 1 are free starting points, so their value is just their own cost.
- 3
Transition: cost[i] + the cheaper of the two steps you could have jumped from.
- 4
The top floor is beyond the last index — finish with min of the last two steps.
- 5
Final version keeps only prev2 and prev instead of the full array.
How I solved it · from first idea to final
dfs(index) with a dict cache, called for the last two steps.
dp array filled from index 2 forward.
prev2 / prev roll forward; the dp list is left unused.
Recursion + memoPythonO(n) time · O(n) space
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
memo = {}
def dfs(index):
#if current index is at 1 or 0, we can reach the steps in cost[0]
#cost to reach the current index
#current step cost + min cost to reach previous steps
#cost[index]+dfs(index)
if index<0:
return 0
if index==0:
return cost[0]
if index == 1:
return cost[1]
if index in memo:
return memo[index]
cur_cost = cost[index]+min(dfs(index-1),dfs(index-2))
memo[index]=cur_cost
return memo[index]
#either I can start at last step/or either I can start at second last step
return min(dfs(n-1),dfs(n-2))TabulationPythonO(n) time · O(n) space
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [float("inf")]*n
dp[0]=cost[0]
dp[1]=cost[1]
for index in range(2,n):
dp[index]=cost[index]+min(dp[index-1],dp[index-2])
return min(dp[n-1],dp[n-2])Two variablesPythonO(n) time · O(1) space
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [float("inf")]*n
prev2=cost[0]
prev=cost[1]
"""
prev2 prev cur
prev2 prev
"""
for index in range(2,n):
cur=cost[index]+min(prev,prev2)
prev2 = prev
prev=cur
return min(prev,prev2)Min Cost Climbing Stairs — FAQ
What is the intuition behind Min Cost Climbing Stairs?
Think of dp[i] as the cheapest total to stand on step i having paid its cost. You can only arrive from i-1 or i-2, so take the cheaper of those and add cost[i]. The top is one step past the end, reachable from either of the last two steps.
How do you solve Min Cost Climbing Stairs (LeetCode 746)?
1. State: dfs(i) / dp[i] = minimum cost paid to stand on step i. 2. Base cases: steps 0 and 1 are free starting points, so their value is just their own cost. 3. Transition: cost[i] + the cheaper of the two steps you could have jumped from. 4. The top floor is beyond the last index — finish with min of the last two steps. 5. Final version keeps only prev2 and prev instead of the full array.
What is the time and space complexity of Min Cost Climbing Stairs?
Time: O(n). Space: O(1). (Accepted approach: Two variables.)
What are common mistakes in Min Cost Climbing Stairs?
The answer is not dp[n-1] alone — you can step to the top from n-2 as well. Starting at index 0 or 1 is free; don't add an extra step cost.
Is there more than one way to solve Min Cost Climbing Stairs?
Recursion + memo (O(n) time · O(n) space): dfs(index) with a dict cache, called for the last two steps. Tabulation (O(n) time · O(n) space): dp array filled from index 2 forward. Two variables (O(n) time · O(1) space): prev2 / prev roll forward; the dp list is left unused.