mediumDynamic ProgrammingDynamic Programming

Min Cost Climbing Stairs

mediumTime: O(n)Space: O(1)

Recognize the pattern

minimum cost to reach the topcan climb 1 or 2 stepschoose cheaper path

Brute force idea

A straightforward first read of Min Cost Climbing Stairs is this: Try every combination of 1-step and 2-step moves. Each position branches into two choices. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

The real unlock in Min Cost Climbing Stairs comes when you notice this: DP: dp[i] = cost[i] + min(dp[i-1], dp[i-2]). The cost to reach step i is its own cost plus the cheaper of the two steps that could reach it. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Min Cost Climbing Stairs is this: At each step, you arrived from either i-1 or i-2 — take the cheaper one. This greedy-like choice is valid because there are no future consequences beyond the immediate cost. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

The trap in Min Cost Climbing Stairs usually looks like this: Confusing where you start and end — you can start from index 0 OR 1, and the top is one step past the last index. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Dynamic Programming Pattern