How will I solve this using DP?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding Dynamic Programming
Dynamic Programming (DP) is a powerful technique used in computer science to solve complex problems by breaking them down into simpler subproblems. It's particularly useful for optimization problems and problems that exhibit overlapping subproblems and optimal substructure. Essentially, DP is a method for efficiently solving problems with a recursive nature but at a much lower computational cost.
Below, we'll explore how to solve problems using Dynamic Programming, complete with examples and explanations.
Fundamental Concepts of Dynamic Programming
- Optimal Substructure: A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to its subproblems. This characteristic allows us to recursively break the problem into smaller, manageable subproblems.
- Overlapping Subproblems: This occurs when recursive algorithms revisit the same problem multiple times. DP takes advantage of this by storing solutions to subproblems to avoid recomputing them.
- Memoization vs. Tabulation:
- Memoization is a top-down approach where we store results of expensive function calls and reuse the cache when the same inputs occur again.
- Tabulation is a bottom-up approach where we solve all possible small subproblems and build up to solve the larger problem.
Steps to Solve a Problem Using Dynamic Programming
- Characterize the Structure of the Optimal Solution: Understand and describe how a solution can be constructed for the problem using solutions to its subproblems.
- Define the Recurrence Relation: This involves establishing a recursive relationship that breaks down the problem into subproblems. This is a crucial part of developing the dynamic programming solution.
- Compute the Value of an Optimal Solution: Use either memoization or tabulation to evaluate the recurrence relation and store the solution for subproblems to ensure they do not need to be solved more than once.
- Construct an Optimal Solution from the Computed Information: Finally, use the stored information to reconstruct the solution for the original problem.
Example: Fibonacci Numbers
The classic example to illustrate DP is the Fibonacci sequence, where each number is the sum of the two preceding ones.
Recursive Solution (Inefficient)
Related reading
- How would you write a program to generate Haiku?
- Howto create combinations of several vectors without hardcoding loops in C?
- HTML Table rendering algorithms, recommended reading?
- Huffman trees for non-binary alphabets?
- Hungarian Algorithm finding minimum number of lines to cover zeroes?
- Hungarian algorithm multiple jobs per worker
- I am looking for a radio advertising scheduling algorithm / example / experience
- I do not understand the concept of Non Deterministic Turing Machine

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.