MEDIUM NC#100 Dynamic Programming 1D

746. Min Cost Climbing Stairs

📖 Problem

Given an array cost where cost[i] is cost of i-th step, you can start from step 0 or step 1. Find minimum cost to reach top of floor (beyond last step).

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • Array DP table updates
  • State transition thinking
  • Base case initialization

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply Dynamic Programming reasoning pattern
  • Apply Recursion reasoning pattern

💡 Approach

  • dp[i] = min cost to reach step i
  • dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2])
  • Can start from step 0 or 1 with no initial cost
  • Time: O(n), Space: O(n) or O(1) optimized

🛠️ Hints & Pitfalls

Hints

  • dp[i] = min cost to reach step i
  • dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2])
  • Can start from step 0 or 1 with no initial cost

Common Pitfalls

  • Time: O(n), Space: O(n) or O(1) optimized

🧪 Test Cases

Test Case 1
Not run
Input:
minCostClimbingStairs([10,15,20]);
Expected:
15
Test Case 2
Not run
Input:
minCostClimbingStairs([1,100,1,1,1,100,1,1,100,1]);
Expected:
6
Test Case 3
Not run
Input:
minCostClimbingStairs(cost: number[]);
Expected:
Computed from hidden reference

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit