Illustrated · 1 min
Dynamic programming is a method of solving problems by breaking them into smaller sub-problems and solving each of these.
Consider the Towers of Hanoi problem: we have three pegs and disks of various sizes stacked from largest at the bottom and smallest at the top (each one has a hole in the center). This is depicted in the diagram below. The goal is to move the stack of disks from peg to peg . But there's a catch: we cannot put a larger disk on top of a smaller disk.

The Towers of Hanoi setup.
We will solve this with dynamic programming. In fact, let's say we want to program that solves the Towers of Hanoi problem in steps, where is the number of disks initially. How can we break this into smaller sub-problems?
Well, if we can find a way to transfer the bottom disk to , then we will be able to solve it in steps. However, there is one easy way to do this: move the first disks to , then move the bottom one to , then move the disks from to . This will take steps. Also, , so it's easy to see we can solve the problem in steps!