Dynamic programming: core concepts
medium 18 minDynamic programming solves a problem by breaking it into smaller subproblems, solving each distinct subproblem once and storing the answer.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What are the two main properties that make a problem suitable for dynamic programming?
What is the difference between memoisation and tabulation?
Why does naive recursive Fibonacci take exponential time?
In the MOST mnemonic for dynamic programming, what does the S stand for?
Can you use DP if the subproblems never overlap?
Which approach is better for avoiding stack overflow on large inputs?
A DP has a state dp[i][w] for i up to n and w up to W, and each state is computed in O(1). What is the time?
dp[i] = 1 + max(dp[j]) over all j < i with a[j] < a[i]. What is the time for an array of length n?
Moving only right or down, how many paths cross a 3 × 4 grid from the top-left to the bottom-right cell?
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]). What is the least extra space needed to compute dp[n]?
Pick an answer to see why.
Not quite. DP stores answers to subproblems that repeat (overlap) and builds the best answer from best sub-answers (optimal substructure). Disjoint subproblems are divide and conquer; a greedy choice property means greedy suffices.
Not quite. Both compute each state once and have the same big-O time. Memoisation keeps the recursion and caches results; tabulation removes recursion and fills states in dependency order.
Not quite. Each call branches into two and nothing is remembered, so fib(n) makes 2F(n + 1) - 1 calls, which grows like 1.618ⁿ (within O(2ⁿ)). The depth is only n.
Not quite. MOST: Memoise to avoid recomputation, Optimal substructure, Subproblems overlap, Table stores results.
Not quite. Without repeats every subproblem is solved once anyway. Merge sort and factorial are examples: a memo would only cost memory.
Not quite. Tabulation uses loops, so there is no recursion depth. Memoised recursion still needs one frame per level and hits Python's limit of about 1,000.
Not quite. Time = states × work per state = (n × W) × O(1) = O(nW). This is 0/1 knapsack.
Not quite. There are n states, but state i scans up to i earlier states, so the total is 1 + 2 + ... + n = O(n²).
Not quite. Fill dp[r][c] = dp[r - 1][c] + dp[r][c - 1] with 1s along the top row and left column: the rows are 1 1 1 1, 1 2 3 4, 1 3 6 10. It also equals C(5, 2) = 10.
Not quite. Each state reads only the previous two, so two rolling variables replace the table.
Flashcards
Say your answer, then reveal it.
Card 1 of 10
What two properties must a problem have for dynamic programming to help?
Overlapping subproblems (the same subproblems are solved many times) and optimal substructure (the best answer is built from best answers to subproblems).
Say your answer, then reveal it.
What are the four decisions in every DP?
State (what a subproblem means), transition (how it is built from smaller states), base cases (answers known outright) and evaluation order (inputs before the cells that use them).
Say your answer, then reveal it.
Memoisation vs tabulation in one line each?
Memoisation: top-down recursion plus a cache, visits only reachable states. Tabulation: bottom-up loops fill a table in dependency order, no recursion.
Say your answer, then reveal it.
How do you count the time of a DP?
Number of distinct states times the work per state. Unique paths: mn states × O(1) = O(mn). LIS: n states × O(n) = O(n²).
Say your answer, then reveal it.
When can a 1D DP drop from O(n) space to O(1)?
When each state reads only a fixed number of earlier states, such as dp[i - 1] and dp[i - 2]. Keep those as rolling variables.
Say your answer, then reveal it.
How many calls do naive and memoised fib(10) make?
Naive: 177 (2F(n + 1) - 1). Memoised: 19 (2n - 1).
Say your answer, then reveal it.
Why is merge sort not dynamic programming?
Its subproblems (the two halves) never overlap, so storing their answers saves nothing. It is divide and conquer.
Say your answer, then reveal it.
Which DP style avoids stack overflow?
Bottom-up tabulation: it uses loops, so there is no recursion depth to exceed.
Say your answer, then reveal it.
What does the MOST mnemonic stand for?
Memoise to avoid recomputation, Optimal substructure, Subproblems overlap, Table stores results.
Say your answer, then reveal it.
What do you lose when you roll a DP table into a few variables?
The history: you can no longer backtrack through the table to recover the actual choices or path.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.