Lessons 1–5 · About 60 min per lesson

Dynamic programming: states and transitions

Derive a dynamic program from overlapping subproblems and check the definition on small inputs.

Lesson breakdown

  1. Lesson 1

    Identify overlapping subproblems in recursive enumeration.

  2. Lesson 2

    Define dp[i] in one sentence and specify what i represents.

  3. Lesson 3

    Mark base cases, unreachable states and dependencies.

  4. Lesson 4

    Convert memoised recursion into a table evaluated in dependency order.

  5. Lesson 5

    Check a transition against a hand trace and exhaustive enumeration.

Worked example

Climb 1 or 2 stairs at a time. How many ordered routes reach stair 4?

How to solve it

Let dp[i] count routes to i. With dp[0]=1 and dp[1]=1, adding the previous two gives 2, 3 and 5, so the answer is 5.

Assignment

Count routes to the final stair by DP when some landings are blocked. Stair 0 is always available.

Check your result

n=4 with stair 2 blocked → 1; stair 4 blocked → 0; n=0 → 1.