Intermediate Topic 25 of 51 Algorithms
Dynamic Programming
Solving overlapping subproblems once and storing results.
How it works
Dynamic programming solves problems with overlapping subproblems and optimal substructure by storing the answers to subproblems so each is computed once. It can be written top-down with memoization or bottom-up by filling a table in dependency order. Designing a solution means defining the state, writing the recurrence and finding the base cases and evaluation order.
Where it is used
Shortest paths, knapsack, longest common subsequence, edit distance, matrix-chain multiplication, text justification, sequence alignment in bioinformatics and many optimization problems in scheduling and finance use dynamic programming.
Common pitfall
Choosing the wrong state leaves out information or explodes the table size. Off-by-one errors in indexes and base cases are common. Memory can often be reduced with rolling arrays. Do not use DP when subproblems do not overlap; plain recursion or greedy may be simpler.
Where it fits in the course
Dynamic Programming is topic 25 of 51 in Algorithms, rated Intermediate. It follows Greedy Algorithms. The next topic is Knapsack Problem.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the DP Table (LCS) visualizer: Watch the longest common subsequence table fill.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Dynamic Programming?
Where do people use Dynamic Programming?
What is the most common mistake with Dynamic Programming?
What should I learn before this topic?
How can I practice it?
Is Dynamic Programming hard to learn?
How long does it take to learn Dynamic Programming?
Which topics are related to Dynamic Programming?
What should I study after Dynamic Programming?
Related topics
- Greedy Algorithms (Algorithms)
- Knapsack Problem (Algorithms)
- Master Theorem (Algorithms)
- Longest Common Subsequence (Algorithms)
- Dynamic Arrays (Data Structures)
- Programming Paradigms (Languages & Compilers)
- Static vs Dynamic Typing (Languages & Compilers)