Courses / Computer Science I
Algorithmics

Dynamic Programming Intuition

Computer Science I 247 words Free to read

What is Dynamic Programming?

Dynamic programming (DP) solves problems with overlapping subproblems—where the exact same smaller problem is needed many times. Instead of recomputing answers, you solve each subproblem once and store its answer.

Take the naive recursive Fibonacci formula F(n)=F(n1)+F(n2)F(n) = F(n-1) + F(n-2). Plain recursion recalculates F(3)F(3) twice and F(2)F(2) three times, causing exponential explosion. Because there are only nn distinct subproblems, storing them collapses the cost to O(n)O(n) time.

StyleDirectionHow It Works
MemoizationTop-downNatural recursion that caches results on the first call
TabulationBottom-upFills a table from smallest to largest subproblem

DP requires two core features: overlapping subproblems and optimal substructure, where an optimal solution builds directly on optimal subproblem solutions.

Fibonacci's call tree, with its own repeat caught and cached

DP vs. Divide-and-Conquer

Students often confuse dynamic programming with divide-and-conquer. The distinction rests entirely on whether subproblems share work.

FeatureDivide-and-ConquerDynamic Programming
SubproblemsIndependent (e.g., merge sort halves)Overlapping (reused constantly)
Core GoalDivide and combineStore and reuse results

Common pitfall: Applying divide-and-conquer logic to a problem with overlapping subproblems, which triggers catastrophic redundant work.

Classic DP applications include shortest paths, edit distance, the knapsack problem, and longest common subsequence. Each turns an intractable exponential brute-force search into an efficient polynomial-time solution simply by remembering past subresults.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes

Algorithmics