iq.lab
Python starts when a code cell comes near or you run one

Module 12 · 5 lessons · about 9 hours 10 minutes

Dynamic programming

From slow recursion to memoization, one-dimensional tables, grids and two strings, choices with a budget, and a recipe for finding the state of any DP problem.

Lessons
0/5
Exercises
0/36
Code
0/5
Problems
0/20
Checkpoint
Not taken

Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.

  1. 1From recursion to memoizationA recursion that asks the same smaller question again and again can take exponential time. Write each answer down the first time you work it out, and each question is answered once, so Fibonacci drops from exponential to linear time. That one change, memoization, is the first of the two ways to do dynamic programming.1 h 25 min
  2. 2One-dimensional DPWrite the answers to smaller questions in a list, smallest first, and build each new answer from the ones before it. One recipe (state, transition, base cases, answer, order) solves house robber, coin change and decode ways.1 h 55 min
  3. 3Grids and two stringsSome subproblems need two numbers to name them, a row and a column of a grid, or how far along each of two strings you are. The table gets a second index, each cell reads a few neighbors that are already filled, and filling row by row answers unique paths, longest common subsequence and edit distance.2 h
  4. 4Choices with a budgetMany interview problems hand you items to take or leave under a limit. A table of the best answer for every budget compares every choice without listing them, and small changes to it solve equal-sum splits and coin combinations. A second table, one cell per place a subsequence can end, finds the longest increasing subsequence.2 h
  5. 5How to find the stateWhen a problem does not come with its table, write the slow recursion that tries every choice. Whatever changes between its calls is the state. Memoize it, fill it as a table, keep only the rows you need, and walk the table back to recover the choices.1 h 50 min
  6. Checkpoint11 questions. Pass with 80% to finish the module.

Problems for this module

The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.

Next moduleGreedy and a little math