Browse curriculum

Dynamic Programming

The Dynamic Programming Pattern

Solve each distinct subproblem once and reuse the answer. Memoization caches results on top of the natural recursion; tabulation fills a table bottom-up. Both depend on finding a recurrence that expresses a problem in terms of smaller versions of itself.

12

Interactive problems

2

Free to open

Recognise

How do you recognise a dynamic programming problem?

The problem asks for a count of ways, a minimum or maximum over choices, or whether something is achievable — and a brute-force recursion would solve the same subproblem repeatedly. Overlapping subproblems plus optimal substructure is the signature.

Complexity

What is the time complexity of the dynamic programming pattern?

Usually O(states × transitions). A 1D DP over n items is O(n); knapsack and most string DP are O(n × m). Space often reduces to one or two rows.

Problems

Dynamic Programming practice problems

Each problem pairs a worked explanation with an interactive visualizer you step through yourself, plus the implementation in JavaScript, Python, Java and C++.

Complexity

Dynamic Programming problems by difficulty and complexity

The difficulty, running time and extra space of every dynamic programming solution on this page, as each lesson derives them.

LessonDifficultyBestAverageWorstSpace
Intro to Dynamic ProgrammingMediumΩ(n)Θ(n)O(n)O(n)
Solving a Question with DPMediumΩ(n)Θ(n)O(n)O(n)
Counting BitsEasyΩ(n)Θ(n)O(n)O(1)
House RobberMediumΩ(n)Θ(n)O(n)O(n)
House Robber IIMediumΩ(n)Θ(n)O(n)O(1)
Coin ChangeMediumΩ(amount · k)Θ(amount · k)O(amount · k)O(amount)
Decode WaysMediumΩ(n)Θ(n)O(n)O(n)
Maximal SquareMediumΩ(m · n)Θ(m · n)O(m · n)O(m · n)
Unique PathsMediumΩ(m · n)Θ(m · n)O(m · n)O(m · n)
Longest Increasing SubsequenceMediumΩ(n²)Θ(n²)O(n²)O(n)
Word BreakMediumΩ(n)Θ(n² · k)O(n² · k)O(n + d)
Maximum Profit in Job SchedulingHardΩ(n log n)Θ(n log n)O(n log n)O(n)
Curriculum

The underlying technique is covered from first principles in the dynamic programming lesson in the DSA visualizer curriculum. Dynamic Programming is one of the 17 coding interview patterns, and its problems also appear in the DSA 75 interview sprint.

Directory

Every other coding interview pattern