Min Cost Climbing Stairs

You already know how to count paths up a staircase. Now each stair carries a price tag. One stair costs 1, the next costs 100, then back to 1 — and the pattern is designed to fool you.

Your first instinct is probably greedy: always step to the cheaper next stair. That instinct feels airtight. On a three-stair case you can verify it by eye. On ten stairs it leads you astray — and the exact stair where it fails is the whole lesson.

The shift from counting paths to minimizing cost is one operator swap inside a recurrence you already know. By the end you'll have the recurrence, a filled DP table, a two-variable space-compressed version, and a second approach that starts from the top and works down — all from the same formula.

Warmup: three stairs

Costs 101520. You may start at stair 0 or stair 1. From any stair, step 1 or 2 forward — or step off the top if it's within reach. You pay the cost of every stair you land on. What is the cheapest way past the top?