Dynamic programming explained in plain terms means understanding how to stop solving the same small problem a thousand times. The central question is why some problems that look impossibly large become easy once you notice their answers repeat, and the answer is to solve each subproblem once, remember the result, and reuse it whenever it is needed again.
Dynamic programming is a method for solving problems by breaking them into overlapping subproblems, solving each one once, and storing the answers so that larger problems can be built from them. It turns exponential brute force into polynomial time for a huge class of problems, from sequence alignment to route planning, and it is one of the most valuable techniques in a programmer’s toolkit.
The idea has a reputation for being hard, and that reputation is mostly undeserved: once you can spot the subproblems, the rest is bookkeeping. This article explains what dynamic programming is, the two ways to implement it, and the classic patterns that cover most problems you will ever meet.
Key takeaways
- Dynamic programming solves each subproblem once and reuses the answer.
- It needs overlapping subproblems and optimal substructure to work.
- Memoisation stores answers top-down, tabulation builds them bottom-up.
- It turns exponential brute force into polynomial time.
- A handful of patterns cover most dynamic programming problems.
What dynamic programming is
Dynamic programming applies to problems with two properties. The first is overlapping subproblems, which means the same small problem appears again and again inside the larger one, and the second is optimal substructure, which means the best answer to the large problem can be built from the best answers to its smaller parts. When both hold, remembering answers is not just an optimisation, it changes the complexity class of the problem.
The Fibonacci numbers are the classic first example, and they show the effect clearly. Computing the fiftieth Fibonacci number by naive recursion repeats the same calls millions of times, because each call branches into two, and the two branches share most of their work. Storing each result the first time it is computed collapses the whole calculation into fifty steps, which is the difference between a program that finishes instantly and one that never finishes at all.
The name is a historical accident. Richard Bellman coined it in the 1950s, partly because the word programming then meant planning and scheduling rather than coding, and partly, by his own admission, because dynamic sounded impressive enough to shield his research from a secretary of defence who disliked mathematical research. Our guide to decision trees explains a different way of breaking a hard choice into smaller decisions.
Memoisation versus tabulation
There are two ways to implement dynamic programming, and they are the same idea viewed from opposite ends. Memoisation works top-down: write the natural recursive solution, then add a cache that stores each result the first time it is computed and returns the stored answer on every later call. The code stays close to the problem’s definition, and only the subproblems that are actually needed get solved.
Tabulation works bottom-up: identify the smallest subproblems, solve them first, and fill a table in an order that guarantees every answer is ready before it is needed. The code is usually a loop rather than a recursion, which avoids stack limits and often uses less memory, because the table can sometimes be shrunk to a few rows once earlier rows are no longer needed.
The choice between them is mostly a matter of taste and constraints. Memoisation is easier to write correctly, because it follows the recursive structure of the problem, while tabulation is easier to optimise, because the order of computation is explicit and the memory can be trimmed. Most experienced programmers reach for memoisation first and convert to tabulation only when performance demands it.
The classic patterns
Most dynamic programming problems fall into a handful of patterns, and recognising the pattern is most of the work. The knapsack pattern asks how to fill a limited capacity with the most valuable items, and it appears whenever a resource is scarce, from cargo loading to budget allocation. The longest common subsequence pattern compares two sequences and finds what they share, which is the heart of diff tools and DNA alignment.
The shortest path pattern appears whenever a journey can be split at intermediate stops, and it covers everything from road networks to editing one string into another, where each edit is a step with a cost. The counting pattern asks how many ways something can be done, like climbing stairs or making change, and its answers grow fast, which is why the reuse of subproblems matters so much.
The interval pattern solves problems on a stretch of data by trying every possible split point, which is how optimal matrix multiplication ordering and the best way to parenthesise an expression are found. The tree pattern solves problems on a hierarchy by combining the answers of children into the answer of the parent, which is how many scheduling and placement problems are tackled.
Learning these patterns matters more than memorising individual solutions, because a new problem is rarely new in structure. A programmer who recognises the knapsack shape in a resource allocation task, or the subsequence shape in a version comparison, already knows how to solve it before writing a line of code.

Dynamic programming versus other approaches
Dynamic programming is one of several ways to tackle a hard problem, and each has its place. Brute force tries everything and is simple but slow, greedy algorithms decide locally and are fast but often wrong, and divide and conquer splits problems into independent parts rather than overlapping ones. The right approach follows from the structure of the problem.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Dynamic programming | Greedy algorithm | Divide and conquer |
|---|---|---|---|
| Subproblems | Overlapping, solved once each | None, one local choice | Independent, solved separately |
| Correctness | Optimal when structure holds | Optimal only for special cases | Always correct by construction |
| Typical speed | Polynomial after reuse | Fast, often linearithmic | Logarithmic to linearithmic |
| Memory used | Table of subproblem answers | Almost none | Recursion stack only |
| Best for | Optimisation with repetition | Problems with greedy choice | Sorting, searching, splitting |
The practical result is that dynamic programming is the answer when a problem repeats its own subproblems, greedy is the answer when a local choice is provably safe, and divide and conquer is the answer when the parts never overlap.
Where dynamic programming is used
Bioinformatics runs on dynamic programming. Aligning two DNA sequences, finding the genes in a genome, and comparing proteins all reduce to sequence comparison, and the algorithms that do it are dynamic programming over a grid of subproblems. The Needleman-Wunsch and Smith-Waterman algorithms are the textbook examples, and they are used daily in labs worldwide.
Compilers use dynamic programming for instruction selection and register allocation, where the cheapest way to implement an expression is built from the cheapest ways to implement its parts. Our guide to compilers explains the pipeline these optimisations sit inside, and the same table-filling idea appears in parsing and code generation.
Operations research uses dynamic programming for inventory control, production planning, and equipment replacement, where a decision today affects the options available tomorrow. The method’s origins are in exactly these planning problems, and it remains the standard tool for sequential decisions under uncertainty.
Machine learning uses dynamic programming inside sequence models, where the Viterbi algorithm finds the most likely sequence of hidden states in speech recognition and natural language processing. Our guide to cryptography shows another field where the same table-driven thinking turns an impossible search into a routine computation.
Strengths and limits
The greatest strength of dynamic programming is the size of the speedup it delivers. Turning an exponential search into a polynomial one is not a marginal gain, it is the difference between a problem that cannot be solved at all and one that finishes before lunch, and that transformation comes from a single idea: remember what you have already computed.
It is also a strength that the method is general. The same pattern of overlapping subproblems and stored answers applies to strings, graphs, trees, knapsacks, and schedules, which makes it one of the few techniques that repays learning once and applying everywhere.
The limits are real, though. Dynamic programming needs both overlapping subproblems and optimal substructure, and when either is missing, the method either gives the wrong answer or saves nothing. The table of answers can also grow large, and for some problems the memory cost is the real constraint rather than the time.
Dynamic programming checklist
- Check for overlapping subproblems before reaching for the method.
- Confirm the optimal answer builds from optimal smaller answers.
- Start with memoisation, it follows the recursive structure.
- Convert to tabulation when memory or stack limits demand it.
- Define the state clearly before writing any code.
- Work through a small example by hand first.
- Watch for off-by-one errors in table boundaries.
- Consider shrinking the table to a few rows when possible.
- Recognise the pattern before designing the solution.
Frequently asked questions
What is dynamic programming in simple terms?
It is a method that breaks a problem into smaller problems that repeat, solves each one once, and reuses the answers to build the full solution.
What is the difference between memoisation and tabulation?
Memoisation works top-down with recursion and a cache, while tabulation works bottom-up by filling a table in order, and both store each subproblem answer once.
What is optimal substructure?
It is the property that the best answer to a problem can be built from the best answers to its smaller parts, which dynamic programming depends on.
How is dynamic programming different from divide and conquer?
Divide and conquer splits a problem into independent parts, while dynamic programming handles parts that overlap and would otherwise be solved many times.
Can dynamic programming solve every optimisation problem?
No, it only works when the problem has overlapping subproblems and optimal substructure, and many problems have neither.
When should I not use dynamic programming?
When the subproblems do not repeat, or when a greedy choice is provably correct, because a simpler method will be faster and use less memory.
The bottom line
Dynamic programming explained in one line is this: never solve the same small problem twice, and the impossible becomes routine.
It is the technique that turns exponential searches into practical programs, and the reason so many hard problems have such short solutions.
This article is general information about how dynamic programming works, not advice about choosing an algorithm for a particular system.







