Dynamic programming (DP) solves a problem by breaking it into overlapping subproblems, solving each subproblem once, and storing the result so it is never recomputed. In C you implement it in one of two ways: top-down memoisation, which is recursion plus a cache, or bottom-up tabulation, which fills an array in order.
A problem is a good fit for DP when it has optimal substructure (the best answer is built from best answers to smaller problems) and overlapping subproblems (the same smaller problems come up repeatedly). Naive Fibonacci is the textbook case: it takes exponential time recursively and linear time with DP.
| Approach | How it works | Strength | Watch out for |
|---|---|---|---|
| Top-down (memoisation) | Recursion with a cache checked before computing | Computes only the states you need | Deep recursion can overflow the stack |
| Bottom-up (tabulation) | Loop that fills a table from the smallest state up | No recursion, easy to shrink memory | You must get the fill order right |
To design a DP solution, define the state (what the subproblem is), write the recurrence (how a state depends on smaller states), set the base cases, and decide the order of evaluation.
The recurrence is F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1. The cache turns an exponential computation into O(n). The result grows fast, so the code uses unsigned long long, which holds values up to F(93).
A separate computed array is used instead of a sentinel value so that every possible result is valid. Because the recurrence for F(n) only needs the previous two values, the bottom-up version can use two variables and O(1) space.
The longest common subsequence (LCS) of two strings is the longest sequence of characters that appears in both in the same order, not necessarily contiguously. Let dp[i][j] be the LCS length of the first i characters of X and the first j characters of Y:
dp[i][j] = 0.X[i-1] == Y[j-1], then dp[i][j] = dp[i-1][j-1] + 1.dp[i][j] = max(dp[i-1][j], dp[i][j-1]).Row 0 and column 0 stay zero because calloc zero-fills the table. Time and space are both O(m * n). Since each row only depends on the previous row, you can cut memory to O(min(m, n)) if you need only the length.
Given items with a weight and a value and a bag of limited capacity, the 0/1 knapsack problem asks for the maximum total value when each item is taken at most once. Let best[w] be the best value achievable with capacity w.
The best choice is the items with weights 3 and 4 (values 4 and 5). Looping the capacity downwards is the detail that matters: iterating upwards would let the same item be counted several times, which solves the unbounded knapsack instead. Time is O(items * capacity) and space is O(capacity).
int dp[m + 1][n + 1] live on the stack and can overflow it for large inputs. They are also optional since C11. Allocate large tables on the heap.calloc, memset or an explicit flag array.Memoisation is top-down: recurse and cache results on demand. Tabulation is bottom-up: fill a table from the base cases using loops. Both have the same asymptotic cost for the same states.
If a recursive solution recomputes the same arguments many times and the best answer depends on best answers to smaller inputs, DP applies.
Store a choice or parent for each state, or walk the table backwards from the final state, as you would to print the actual common subsequence.
No. Divide and conquer splits into independent subproblems, while DP reuses overlapping ones.
Dynamic programming stores results of overlapping subproblems so each is computed once. In C, choose between memoised recursion and a bottom-up table, allocate large tables on the heap, pick integer types that cannot overflow, and iterate in the right order. Fibonacci, LCS and knapsack cover the main patterns.