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: Find Cheapest Path to Top101520TOPStep 0Step 1Step 2STARTOptimal PathCost: 15Expensive: 10+20=30Choose path with minimum total cost
Understanding the Visualization
1
Input
Array of step costs, can start from index 0 or 1
2
Process
At each step, choose cheaper of two previous paths
3
Output
Minimum cost to reach beyond the last step
Key Takeaway
🎯 Key Insight: At each step, we only need to consider the cheaper of the two previous steps to build the optimal solution
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