Min Cost Climbing Stairs - Problem

Imagine you're climbing a staircase where each step has a toll fee you must pay before moving forward. You're given an integer array cost where cost[i] represents the price to step on the i-th step.

Here's the twist: once you pay the cost for a step, you can either climb one step or two steps forward. You have the flexibility to start your journey from either step 0 or step 1 (both are free to start on).

Goal: Find the minimum cost to reach the "top of the floor" (which is one position beyond the last step in the array).

Example: If cost = [10, 15, 20], you want to reach position 3 (beyond the array). You could start at step 1 (cost 15), then jump 2 steps to reach the top, for a total cost of 15.

Input & Output

example_1.py β€” Small Staircase
$ Input: cost = [10, 15, 20]
β€Ί Output: 15
πŸ’‘ Note: You can start at index 1 (cost 15), then jump 2 steps to reach the top. Total cost = 15. Alternative: start at index 0 (cost 10), jump 1 step to index 1 (cost 15), then jump 2 steps to top. Total cost = 10 + 15 = 25. Choose minimum: 15.
example_2.py β€” Longer Staircase
$ Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 99, 1]
β€Ί Output: 6
πŸ’‘ Note: Optimal path: Start at index 0 (cost 1) β†’ jump 2 steps to index 2 (cost 1) β†’ jump 2 steps to index 4 (cost 1) β†’ jump 2 steps to index 6 (cost 1) β†’ jump 2 steps to index 8 (cost 99) β†’ jump 2 steps to top. Wait, that's wrong. Better path: 0β†’2β†’4β†’6β†’7β†’9β†’top costs 1+1+1+1+1+1=6.
example_3.py β€” Edge Case
$ Input: cost = [5, 10]
β€Ί Output: 5
πŸ’‘ Note: With only 2 steps, we can start at either step 0 (cost 5) or step 1 (cost 10), then jump directly to the top. Choose the minimum starting cost: 5.

Constraints

  • 2 ≀ cost.length ≀ 1000
  • 0 ≀ cost[i] ≀ 999
  • You can start from step index 0 or 1
  • The "top of the floor" is one position beyond the last array element

Visualization

Tap to expand
The Toll Bridge Adventure πŸŒ‰River BankGOAL!$10$15$20Start Option 1Start Option 21 step2 stepsJump to Goal!Optimal StrategyπŸ’‘ Start at Bridge 2 ($15)🦘 Jump 2 bridges to reach goalπŸ’° Total cost: $15Alternative: $10 + $15 = $25βœ“ Choose minimum: $15
Understanding the Visualization
1
Starting Options
You can begin your journey from the first or second bridge for free
2
Bridge Hopping
After paying a toll, you can cross to the next bridge OR skip one and go to the bridge after
3
Smart Choices
At each bridge, choose the cheaper of the two possible previous routes
4
Reach the Goal
Find the minimum total cost to reach the other side of the river
Key Takeaway
🎯 Key Insight: At each step, we only need to know the minimum cost to reach the previous two positions. This allows us to use dynamic programming with constant space complexity!
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
82.3K 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