20 Patterns to Master Dynamic Programming
20 Patterns to Master Dynamic Programming
324 3 24 Share
Dynamic Programming (DP) is arguably the most difficult topic for coding
interviews.
But, like any other topic, the fastest way to learn it is by understanding different
patterns that can help you solve a wide variety of problems.
In this article, I’ll walk you through 20 patterns that will make learning DP much
easier.
I’ll share when to use each pattern and provide links to LeetCode problems you can
practice to learn them better.
I have listed them from easy to hard and also linked resources to learn each pattern.
1. Fibonacci Sequence
The Fibonacci sequence pattern is useful when the solution to a problem depends on
the solutions of smaller instances of the same problem.
There is a clear recursive relationship, often resembling the classic Fibonacci
sequence F(n) = F(n-1) + F(n-2).
LeetCode Problems:
LeetCode 70: Climbing Stairs
2. Kadane's Algorithm
Kadane's Algorithm is primarily used for solving the Maximum Subarray Problem and
its variations where the problem asks to optimize a contiguous subarray within a one-
dimensional numeric array
LeetCode Problems:
LeetCode 53: Maximum Subarray
3. 0/1 Knapsack
3. There's a constraint on the total weight (or some other resource) you can use.
4. You want to maximize (or minimize) the total value of the selected items.
5. Each item can be chosen only once (hence the "0/1" - you either take it or you
don't).
LeetCode Problems:
LeetCode 416: Partition Equal Subset Sum
4. Unbounded Knapsack
The Unbounded Knapsack pattern is useful when:
3. There's a constraint on the total weight (or some other resource) you can use.
4. You can select each item multiple times (unlike 0/1 Knapsack where each item can
be chosen only once).
LeetCode Problems:
LeetCode 322: Coin Change
The Longest Common Subsequence pattern is useful when you are given two
sequences and need to find a subsequence that appears in the same order in both the
given sequences.
LeetCode Problems:
LeetCode 1143: Longest Common Subsequence
The Longest Increasing Subsequence pattern is used to solve problems that involve
finding the longest subsequence of elements in a sequence where the elements are in
increasing order.
LeetCode Problems:
LeetCode 300: Longest Increasing Subsequence
7. Palindromic Subsequence
The Palindromic Subsequence pattern is used when solving problems that involve
finding a subsequence within a sequence (usually a string) that reads the same
forwards and backwards.
LeetCode Problems:
LeetCode 516: Longest Palindromic Subsequence
8. Edit Distance
The Edit Distance pattern is used to solve problems that involve transforming one
sequence (usually a string) into another sequence using a minimum number of
operations.
The allowed operations typically include insertion, deletion, and substitution.
LeetCode Problems:
LeetCode 72: Edit Distance
9. Subset Sum
The Subset Sum pattern is used to solve problems where you need to determine
whether a subset of elements from a given set can sum up to a specific target value.
LeetCode Problems:
LeetCode 416: Partition Equal Subset Sum
You need to optimize some property over these partitions (e.g., minimize cost,
maximize value).
The solution to the overall problem can be built from solutions to subproblems on
smaller substrings.
LeetCode Problems:
LeetCode 139: Word Break
Counting the number of distinct binary search trees that can be formed with n
nodes.
Counting the number of ways to triangulate a polygon with n+2 sides.
LeetCode Problems:
LeetCode 96: Unique Binary Search Trees
LeetCode Problems:
LeetCode 1039: Minimum Score Triangulation of Polygon
1. You need to count the number of different ways to achieve a certain goal or reach
a particular state.
4. The problem can be broken down into smaller subproblems with overlapping
solutions.
5. You're dealing with combinatorial problems that ask "in how many ways" can
something be done.
LeetCode Problems:
LeetCode 91: Decode Ways
14. DP on Grids
The DP on Grids pattern is used to solve problems that involve navigating or
optimizing paths within a grid (2D array).
For these problems, you need to consider multiple directions of movement (e.g., right,
down, diagonal) and solution at each cell depends on the solutions of neighboring
cells.
LeetCode Problems:
LeetCode 62: Unique Paths
15. DP on Trees
The DP on Trees pattern is useful when:
2. The problem can be broken down into solutions of subproblems that are
themselves tree problems.
3. The problem requires making decisions at each node that affect its children or
parent.
4. You need to compute values for nodes based on their children or ancestors.
LeetCode Problems:
LeetCode 337: House Robber III
16. DP on Graphs
The DP on Graphs pattern is useful when:
2. The problem requires finding optimal paths, longest paths, cycles, or other
optimized properties on graphs.
3. You need to compute values for nodes or edges based on their neighbors or
connected components.
LeetCode Problems:
LeetCode 787: Cheapest Flights Within K Stops
LeetCode 1334. Find the City With the Smallest Number of Neighbors at a
Threshold Distance
17. Digit DP
The Digit DP Pattern is useful when:
3. You need to find patterns or properties related to the digits of numbers within a
range.
4. The range of numbers is large (often up to 10^18 or more), making brute force
approaches infeasible.
LeetCode Problems:
LeetCode 357: Count Numbers with Unique Digits
18. Bitmasking DP
The Bitmasking DP pattern is used to solve problems that involve a large number of
states or combinations, where each state can be efficiently represented using bits in
an integer.
LeetCode Problems:
LeetCode 1986: Minimum Number of Work Sessions to Finish the Tasks
LeetCode Problems:
LeetCode 688: Knight Probability in Chessboard
1. The problem can be modeled as a series of states and transitions between these
states.
2. There are clear rules for moving from one state to another.
3. The optimal solution depends on making the best sequence of state transitions.
5. There's a finite number of possible states, and each state can be clearly defined.
LeetCode Problems:
LeetCode 309: Best Time to Buy and Sell Stock with Cooldown
If you found it valuable, hit a like ❤️ and consider subscribing for more such content
every week.
P.S. If you’re finding this newsletter helpful and want to get even more value, consider
becoming a paid subscriber.
As a paid subscriber, you'll receive an exclusive deep dive every week, access to a
comprehensive system design learning resource , and other premium perks.
There are group discounts, gift options, and referral bonuses available.