Cheapest Way Up the Stairs
L8 Hard Dynamic Programming
When the best future depends only on the previous two steps, you can build the answer with two rolling variables instead of a full table.
You are on the floor before index 0 of cost. From index i you may climb 1 or 2 steps into i + 1 or i + 2, paying cost[i] when you land. Reach the floor past the last index (the top) at minimum total cost.
Examples
▸ cost = [10, 15, 20]
→ 15
▸ cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
→ 6
▸ cost = [0, 0, 0, 0]
→ 0