EasyDynamic Programming

Min Cost Climbing Stairs

cost[i] is what you pay to step off floor i; then you climb 1 or 2 floors. You may start on floor 0 or 1. Return the minimum cost to reach the top, just past the last index.

cost = [1,2,3] → 2cost = [1,2,1,2,1,1,1] → 4

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Staircase

$ = cost on each step; the number above it is filled in as it is computed

Time and space complexity

n = len(cost). The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. 2 or more costs from 0 to 100; drawings use at most 5 steps for plain recursion, 10 for top-down and 14 for the loops.