cost[i] is the cost of stepping off step i. You may start on step 0 or 1 and climb one or two steps at a time. Return the minimum cost to reach the top, just past the last step.
Before you code
Write the five decisions first: what dp[i] means, the recurrence, the base cases, the order and where the answer is.
Hints
- dp[i]: cheapest cost to stand on step i; dp[0] = dp[1] = 0.
dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]).- The answer is dp[n]. Then reduce it to two variables.
Production & complexity notes
When solving Min cost climbing stairs on LeetCode (easy), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Bottom-up tables to reinforce the underlying pattern.
What it practices
Turning a recurrence into a table, from Bottom-up tables.