teacher
Sign in

Dynamic Programming Basics

Spot repeated subproblems, then reuse each answer with memoization or a table. Learn to name the state, transition, base case and answer.

40 minCF 1000–15007 interactive7 problems

The problem

You climb a staircase of n steps. Each move goes up 1 or 2 steps. How many different ways reach the top?

Think about the last move. It came from step n - 1 or from step n - 2. So:

ways(n)=ways(n−1)+ways(n−2)ways(n) = ways(n-1) + ways(n-2)

That is a recursive answer. It is correct, and it is very slow.

1 of 9