Browse curriculum

Dynamic Programming

Dynamic Programming, Step by Step

Dynamic programming is where most interview preparation stalls. The obstacle is rarely the code, which is usually short — it is seeing the recurrence, and recognising that a problem has overlapping subproblems in the first place.

These lessons approach DP as a progression rather than a collection of solutions. You start from plain recursion, watch the same subproblem get recomputed, add memoization and see the recomputation disappear, then flip to tabulation and watch the table fill. Working through the classic families — knapsack, sequence, string and tree DP — turns them into recognisable shapes instead of separate problems to memorise.

11

Interactive lessons

1

Free to open

6

Languages (JS, TS, Python, Java, C, C++)

Lessons

Dynamic Programming lessons

Complexity

Dynamic Programming time and space complexity compared

Best, average and worst-case running time and extra space for every dynamic programming lesson, as each lesson derives them.

LessonBestAverageWorstSpace
IntroductionVariableVariableVariable (Depends on State Space)Variable (Usually O(State Space))
Fibonacci SequenceO(N)O(N)O(N) (vs O(2^N) for naive recursion)O(N) (Can be optimized to O(1) space)
Climbing StairsO(N)O(N)O(N)O(N) (Optimizable to O(1))
Unique PathsO(m*n)O(m*n)O(m*n)O(m*n) (Can be optimized to O(n) or O(min(m,n)))
0/1 KnapsackO(N*W)O(N*W)O(N*W)O(N*W) (Optimizable to O(W))
Subset SumO(N*Target)O(N*Target)O(N*Target)O(N*Target) (Optimizable to O(Target))
Longest Common SubsequenceO(M*N)O(M*N)O(M*N)O(M*N)
Longest Increasing SubsequenceΩ(N²)Θ(N²)O(N²)O(N)
Edit DistanceO(M*N)O(M*N)O(M*N)O(M*N)
Max Path SumO(N)O(N)O(N)O(H) (Recursion Stack)
Digit DPO(log N)O(log N * 10)O(log N * 10 * StateSize)O(log N) (Recursion Depth)
Curriculum

Continue the DSA curriculum

Dynamic Programming is one of 12 topics in the interactive DSA visualizer curriculum. Once this topic is comfortable, the coding interview pattern library reorganises the same material around the patterns interview questions are built on.

Directory

Every other DSA visualizer