Dynamic Programming
Turn exponential recursive searches into polynomial algorithms by defining states, transitions and an evaluation order.
5 objectives 6 steps about 140 min
Check what you know
The lessons are coming later. The knowledge check is ready now.
This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.
Learning objectives
- Derive a recurrence from a brute-force recursion and memoize it with functools.cache, naming the state, transitions and base cases.
- Convert a memoized solution to bottom-up tabulation and reduce memory with rolling rows when dependencies allow.
- Solve classic 1D and 2D families (house robber, coin change, 0/1 knapsack, longest increasing subsequence, edit distance, grid paths) and reconstruct an optimal solution, not only its value.
- Compute a DP solution's time and space as states times transitions and check the result against input limits.
- Solve one-pass sequence optimizations with a best-ending-here state, such as maximum subarray sum (Kadane), maximum product subarray and the best single buy-then-sell trade, and explain why O(1) state is enough.
Lesson plan
6 steps, about 140 min · 0 of 5 lessons available so far
- From Brute Force to a Memo Worked example · 25 min · coming later
- Designing States and Transitions Concept · 20 min · coming later
- Bottom-Up Tables and Rolling Rows Worked example · 20 min · coming later
- Knapsack and Coin Change Families Worked example · 25 min · coming later
- Lab: Edit Distance with a Traceback Lab · 30 min · coming later
- Check: DP Problem Set Knowledge check · 10 questions
Practise it
Solve coin change (fewest coins and number of ways), 0/1 knapsack with the chosen items, longest increasing subsequence, and edit distance with the alignment printed; before coding each, write the state, recurrence, base cases and complexity. Add maximum subarray sum with its start and end indices and check it against an O(n^2) scan.
References
Hard prerequisites
Unlocks
On these roadmaps
Progress and evidence for this skill are shared across all of them.