A Dynamic Programming
10 — Algorithms
Solve once — remember forever

Dynamic Programming

Break a hard problem into overlapping subproblems, solve each one only once, and store the answer. Exponential work collapses into polynomial.

Dynamic programming (DP) is not a data structure — it is a technique. It applies when a problem has two traits: overlapping subproblems (the same smaller problem is solved again and again) and optimal substructure (the best overall answer is built from the best answers to its parts). The fix is simple: solve each subproblem once, store the result in a table, and reuse it. You can work top-down (recursion plus a cache — memoization) or bottom-up (fill a table from the smallest pieces — tabulation). Either way, exponential recomputation becomes a single pass.

Try it in 3D — minimum path sum

Each tower is a grid cell. The goal: cheapest path from the back corner to the front, moving only right or forward. Each cell's height becomes its cost + the cheaper of its two solved neighbors.

Drag to rotate — scroll to zoom
grid 5—5
Press —Fill table— to watch the DP build.ready
Solving now Neighbors used Solved cell Optimal path

The two pillars & two styles

If a problem has both pillars, DP applies — and you can implement it top-down or bottom-up.

Cost cheat sheet

For this grid problem (n cells per side), DP turns exponential recursion into a clean quadratic fill.