Skip to content
Data Structures & Algorithmsadvanced · 75 min

Dynamic Programming

Solve problems by combining solutions to overlapping subproblems. Identify the state, the recurrence, and the base cases; then choose memoization (top-down) or tabulation (bottom-up).

When DP applies

Two signals: optimal substructure (the optimal answer is built from optimal sub-answers) and overlapping subproblems (the same sub-answer is needed repeatedly). Classic examples: Fibonacci, coin change, longest common subsequence, knapsack, edit distance.

Memoization vs tabulation

Top-down memoization writes the natural recursion and caches results by state — easy to derive, uses the call stack. Bottom-up tabulation fills a table in dependency order — avoids recursion limits and often allows space optimisation (e.g. keeping only the last row/value).

Designing the recurrence

Define the state precisely (what do the indices/parameters mean?), write the transition (how does state n depend on smaller states?), and the base cases. Getting the state definition right is 80% of the problem.

Resources

Practice & test yourself

Take the quiz →