Guide · Algorithms · Dynamic programming

Dynamic programming – a guide through the lessons

Dynamic programming (DP) solves problems that break down into overlapping subproblems: each subproblem is solved once, the result is remembered and reused.

The guide goes from memoization to the most important DP patterns that appear at district, national and SIO (Serbian Informatics Olympiad) level. Recursion and backtracking are now a separate area, with their own guide.

  • Grade 8 and secondary school
  • C++
  • Competition preparation
How to recognize DP: the task asks for the best value or the number of ways, and the solution for a bigger input can be built from the solutions for smaller inputs. First define the state (what you remember), then the transition (how it is computed from smaller states).

1 Before dynamic programming: recursion

DP is built on recursion. If recursion is not clear to you, go through its guide first.

2 Introduction to dynamic programming

Overlapping subproblems, memoization (top-down) and tabulation (bottom-up).

3 Basic DP patterns

Three problems that are most often recognized in competition tasks.

4 Advanced DP patterns

DP over a grid, the minimal number of coins and the distance between words.

Suggested order

  1. Introduction to dynamic programming
  2. Fibonacci, memoization and DP
  3. Knapsack
  4. Subset Sum
  5. LCS
  6. Grid DP
  7. Coin Change
  8. Edit Distance

Useful external resources