Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DSA
Data Structures & Algorithms
0/15 completed
Beginner
Big-O and complexityArrays and stringsHash maps and setsTwo pointers and sliding windows
Intermediate
Linked listsStacks and queuesRecursion and backtrackingSorting algorithmsBinary search
Advanced
Trees and binary search treesHeaps and priority queuesGraphs: BFS and DFSDynamic programming
Pro
Shortest paths, topological sort and union-findSolving problems in coding interviews
Tracks / Data Structures & Algorithms / Advanced

Dynamic programming

Dynamic programming (DP) solves problems by breaking them into overlapping subproblems, solving each subproblem once, and reusing the answers. It turns exponential brute force into polynomial time, and it's behind spell checkers, route planners, DNA comparison, diff, and many of the hardest interview questions.

When does DP apply?

Two conditions:

  1. Optimal substructure: the best answer can be built from best answers to smaller problems.
  2. Overlapping subproblems: the same smaller problems come up again and again.

The Fibonacci example from the recursion lesson has both: fib(5) needs fib(4) and fib(3), and fib(4) needs fib(3) again.

Two ways to do it

Python · editable
Top-down (memoisation) Bottom-up (tabulation)
How Recursive, cache each result Loops filling a table
Pros Close to the problem's definition; computes only the states needed No recursion limit; easy to shrink memory
Cons Recursion depth limits in Python Must work out the order of states

The recipe

  1. Define the state: what does dp[i] (or dp[i][j]) mean, in words?
  2. Find the recurrence: how does a state follow from smaller states?
  3. Base cases: the smallest states, answered directly.
  4. Order: compute states so that dependencies come first.
  5. Answer: which state holds the final result?

Coin change

Fewest coins to make an amount. State: dp[a] = fewest coins for amount a. Recurrence: try each coin as the last one.

Python · editable

O(amount × number of coins). The second example shows why DP is needed: the "greedy" approach (always take the biggest coin) gives 3 coins, but 2 is possible.

The 0/1 knapsack

A bag holds a limited weight; each item has a weight and a value, and can be taken at most once. Maximise the value. State: dp[i][w] = best value using the first i items with capacity w.

Python · editable

Strings: longest common subsequence and edit distance

Two-dimensional DP over two strings powers diff, plagiarism detection, DNA alignment and spell checking.

Python · editable

Notice edit_distance("recieve", "receive") is 2: swapping two neighbouring letters counts as two substitutions. The Damerau–Levenshtein variant adds a "transposition" operation costing 1, which matches common typos better.

Longest increasing subsequence

Python · editable

Grid paths

Python · editable

Spotting DP problems

Phrases that suggest DP: "number of ways", "minimum/maximum cost", "longest/shortest … subsequence", "can you reach / make …", where choices at each step affect later choices. Start with a brute-force recursive solution, notice repeated calls, then add memoisation; convert to a table if you need to save memory or avoid deep recursion.

Practice

  1. House robber: maximise the sum of non-adjacent numbers in a list.
  2. Word break: can "applepenapple" be split into words from ["apple", "pen"]?
  3. Count the ways to make an amount with given coins (order doesn't matter).
✎ Check your understanding

1.What two properties make a problem suitable for dynamic programming?

2.Why does greedy fail for coins [1, 3, 4] and amount 6?

3.What does edit distance measure?

← Graphs: BFS and DFS