Dynamic programming (DP) is a way to solve problems that break into smaller problems of the same kind — like recursion — when those smaller problems repeat. Instead of solving the same one again and again, you solve each one once, store its answer, and look it up every time it's needed again. That single idea turns many exponential searches into fast, polynomial-time algorithms.
A problem can be solved with DP when it has both of these:
Almost every DP solution grows from a plain recursion, and you'll see these three versions in every lesson of this course:
The plain recursion: watch how often the same small calls come back.
function fib(n) { if (n <= 1) return n; // base cases: fib(0) = 0, fib(1) = 1 return fib(n - 1) + fib(n - 2); // two calls: the call tree branches}DP's running time is simple to count: the number of distinct subproblems × the work to solve one (given the answers to the smaller ones). Fibonacci has n + 1 subproblems, each one addition: O(n), instead of about 1.6ⁿ calls. The memory is the size of the table — unless you shrink it, which the fourth lesson shows.
problem subproblems work each time
Fibonacci n O(1) O(n)
Climbing stairs n O(1) O(n)
Unique paths in a grid m × n O(1) O(m · n)
Longest common subsequence m × n O(1) O(m · n)
Coin change (amount A) A O(coins) O(A · coins)
Longest increasing subseq. n O(n) O(n²)Was this helpful?