Min Cost Climbing Stairs - Problem

You are given an integer array cost where cost[i] is the cost of the i-th step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

Input & Output

Example 1 — Basic Case
$ Input: cost = [10, 15, 20]
Output: 15
💡 Note: You can start at index 1 (cost 15) and jump directly to the top, avoiding the more expensive paths through step 0 or step 2.
Example 2 — Multiple Steps
$ Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
💡 Note: Start at index 0, jump to index 2, then 4, 6, 8, and finally to the top. Total cost: 1+1+1+1+1+1 = 6.
Example 3 — Minimum Size
$ Input: cost = [5, 10]
Output: 5
💡 Note: Start at index 0 (cost 5) and jump directly to the top, which is cheaper than starting at index 1 (cost 10).

Constraints

  • 2 ≤ cost.length ≤ 1000
  • 0 ≤ cost[i] ≤ 999

Visualization

Tap to expand
Min Cost Climbing Stairs Space-Optimized Dynamic Programming INPUT Step 0 cost=10 Step 1 cost=15 Step 2 cost=20 TOP Input Array: 10 15 20 ALGORITHM STEPS 1 Initialize Variables prev1=0, prev2=0 2 Iterate Through Steps curr = cost[i] + min(prev1, prev2) 3 Update Variables prev2=prev1, prev1=curr 4 Return Result min(prev1, prev2) Trace: i=0: curr=10+min(0,0)=10 prev2=0, prev1=10 i=1: curr=15+min(10,0)=15 prev2=10, prev1=15 Result: min(15,10) = 10 FINAL RESULT Step 0 Step 1 Step 2 TOP +2 Minimum Cost: 15 OK - Optimal! Key Insight: Space-optimized DP uses only O(1) space by tracking just the last two minimum costs. At each step, we can reach from either 1 or 2 steps back, so we take the minimum. Starting from step 1 (cost=15) and jumping 2 steps to top gives us the minimum cost of 15. TutorialsPoint - Min Cost Climbing Stairs | Space-Optimized DP Approach
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 28
85.0K Views
High Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen