What is dynamic programming?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Dynamic programming (DP) is an algorithmic technique employed to solve complex problems by breaking them down into simpler subproblems. It is particularly useful in optimization problems where decisions need to be made to achieve an optimal solution. By leveraging the principle of overlapping subproblems and optimal substructure, dynamic programming avoids redundant calculations and improves computational efficiency.
Key Concepts of Dynamic Programming
Overlapping Subproblems
Dynamic programming is suitable for problems with overlapping subproblems, meaning the problem can be broken down into subproblems which are reused several times. This is a distinguishing feature from divide-and-conquer algorithms that solve independent subproblems.
Optimal Substructure
A problem is said to have an optimal substructure if an optimal solution can be constructed efficiently from optimal solutions of its subproblems. This property ensures that solving each subproblem individually and combining their solutions leads to an optimal overall solution.
Memoization and Tabulation
There are two main approaches to implementing dynamic programming:
- Memoization: This is a top-down approach. Recursive calls compute values for each subproblem and store them in a memory table (often a dictionary or an array) to avoid redundant calculations.
- Tabulation: This is a bottom-up approach. It uses an iterative process to fill up a table where each entry corresponds to a subproblem, solving all subproblems in a systematic manner without recursion.
Examples of Dynamic Programming
Fibonacci Sequence
One of the simplest examples of dynamic programming is the computation of Fibonacci numbers, where each number is the sum of the two preceding ones.
Recursive (inefficient):
Dynamic Programming (Efficient):
0/1 Knapsack Problem
This optimization problem involves selecting a set of items, each with a weight and value, to maximize the total value without exceeding the weight limit. It can be solved optimally using dynamic programming.
Dynamic Programming Solution:
Table Summarizing Key Points
| Feature | Description |
| Overlapping Subproblems | Re-use of identical subproblem solutions |
| Optimal Substructure | Construct global solutions from optimal subproblem solutions |
| Memoization | Top-down approach using recursion and caching of results |
| Tabulation | Bottom-up approach building solutions iteratively |
| Applications | Fibonacci sequence, 0/1 Knapsack, Shortest Path, Longest Common Subsequence, etc. |
Additional Topics
Complexity Analysis
Dynamic programming can significantly alter the time complexity of solving a problem. While a naive recursive approach may have exponential time complexity, dynamic programming often reduces this to polynomial time due to its reuse of solutions to subproblems.
Limitations
While powerful, dynamic programming can have limitations:
- Space Complexity: For problems requiring very large tables, space complexity can become prohibitive.
- Problem Design: Not all problems exhibit overlapping subproblems or optimal substructure, limiting their applicability to dynamic programming solutions.
Advanced Techniques
Advanced techniques, such as the use of bitmasks, state compression, and dividing structures, can extend dynamic programming's applicability even further into more complex problem domains.
In conclusion, dynamic programming is a robust approach for tackling problems with overlapping subproblems and optimal substructure. It provides a framework for transforming naive exponential solutions into efficient polynomial-time solutions by systematically solving smaller subproblems and building up to the desired result.
Related reading
- What is fixed-parameter tractability? Why is it useful?
- What is inductive bias in machine learning?
- What is Microsoft.csharp.dll in .NET 4.0
- what is Newton-Raphson Square Method's time complexity?
- What is gcnew?
- What is it that makes Enum.HasFlag so slow?
- What is Olog N?
- What is plurality classification in decision trees?

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.