Introduction to Dynamic Programming
What is Dynamic Programming?
Dynamic Programming (DP) is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable when the problem has overlapping subproblems and optimal substructure.
Where to Use Dynamic Programming?
DP is commonly used in optimization problems, such as:
- Finding shortest paths in graphs
- Sequence alignment in bioinformatics
- Resource allocation problems
- Knapsack and other combinatorial optimization problems
- Game theory and decision making
How to Use Dynamic Programming?
Break down the main problem into smaller, overlapping subproblems.
Express the solution of the main problem in terms of solutions to subproblems.
Store solutions to subproblems to avoid redundant calculations.
Use stored solutions to build the solution to the original problem.
DP Parent Problems
These are the fundamental DP problems that form the basis for many variations. Mastering these will help you solve a wide range of DP challenges.
All DP Variation Problems
These problems are variations of the parent DP problems. Understanding the connections helps in recognizing patterns and applying solutions effectively.
| Problem | Description | LeetCode |
|---|---|---|
| Fibonacci / Linear Recurrence Variations | ||
| Climbing Stairs | Number of ways to climb n stairs (1 or 2 steps) | #70 |
| Min Cost Climbing Stairs | Cost attached to each stair | #746 |
| House Robber | Max sum without adjacent elements | #198 |
| House Robber II | Circular arrangement | #213 |
| Decode Ways | Count valid string decodings | #91 |
| Knapsack / Subset / Coin Change Variations | ||
| Partition Equal Subset Sum | Check if array can be partitioned into two subsets with equal sum | #416 |
| Target Sum | +/- to reach target | #494 |
| Last Stone Weight II | Partition difference minimization | #1049 |
| Coin Change | Minimum coins to reach amount | #322 |
| Coin Change II | Count combinations to form sum | #518 |
| LCS / String Matching Variations | ||
| Edit Distance | Minimum operations to convert strings | #72 |
| Delete Operation for Two Strings | Deletions to equalize strings | #583 |
| Minimum ASCII Delete Sum | Weighted variant | #712 |
| Longest Palindromic Subsequence | LCS of s and reverse(s) | #516 |
| Grid / Matrix DP Variations | ||
| Unique Paths II | Grid with obstacles | #63 |
| Minimum Path Sum | Min cost path | #64 |
| Dungeon Game | Minimum health required | #174 |
| Minimum Falling Path Sum | From top to bottom | #931 |
| LIS / Subsequence Variations | ||
| Russian Doll Envelopes | 2D LIS variant | #354 |
| Maximum Length of Pair Chain | Pair-based LIS | #646 |
| Number of Longest Increasing Subsequences | Count LIS | #673 |
| Longest Arithmetic Subsequence | Maintain difference state | #1027 |
| Partition DP / MCM Variations | ||
| Burst Balloons | Max coins by choosing partition order | #312 |
| Palindrome Partitioning II | Min cuts to form palindromes | #132 |
| Minimum Score Triangulation | Polygon DP | #1039 |
| Minimum Cost to Cut a Stick | Sort and partition cost | #1547 |
| Tree DP Variations | ||
| Binary Tree Maximum Path Sum | Max path sum in binary tree | #124 |
| House Robber III | Tree version of house robber | #337 |
| Longest Univalue Path | Same value path | #687 |
| Bitmask DP Variations | ||
| Shortest Path Visiting All Nodes | TSP variant | #847 |
| Partition to K Equal Sum Subsets | Subset masking | #698 |
| Matchsticks to Square | DP over subsets | #473 |
Core DP Patterns
Understanding these core patterns will help you recognize which DP approach to apply to new problems.
Summary of Core Patterns
| Category | Parent Problem | Common Variations |
|---|---|---|
| Linear Recurrence | Fibonacci | Climb Stairs, Robber, Decode Ways |
| Knapsack | 0/1 Knapsack | Coin Change, Subset Sum |
| LCS | LCS | Edit Distance, Subsequence |
| Grid | Unique Paths | Minimum Path Sum, Triangle |
| LIS | LIS | Russian Dolls, Bitonic |
| Partition | MCM | Burst Balloons, Palindrome Partition |
| Tree | Diameter | Max Path, Robber III |
| Bitmask | TSP | Equal Subsets, Connect Groups |