All lessons

Dynamic Programming

10 min · C examples

Break a problem into overlapping subproblems, remember answers, and reuse them.

C · Dynamic Programming0/5 cleared

Enemy 1 of 5

This code finds the 10th Fibonacci number and saves each answer it works out, so it never repeats the same work. Press Run to see 55.

Why this works

Dynamic programming (DP) helps when a problem repeats the same smaller question many times. Instead of recomputing, store answers in a table or cache.

Fibonacci is the classic warm-up: fib(n) = fib(n-1) + fib(n-2). A naive recursion explodes; memoization remembers each fib(k) after the first visit.

You
Memory-Leak Rat
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.