Classic DP Master Patterns: 0/1 Knapsack, Coin Change & LCS
Classic DP Master Patterns: 0/1 Knapsack, Coin Change & LCS
To excel in technical coding interviews (FAANG, Tier-1 tech firms), you do not need to memorize hundreds of individual dynamic programming problems. Instead, 90% of interview DP problems derive directly from three foundational archetype patterns:
- 10/1 Knapsack Pattern (Pick or Don't Pick decisions under capacity constraints)
- 2Unbounded Knapsack / Coin Change Pattern (Repetitive item choices, minimization / counting)
- 3Longest Common Subsequence (LCS) Pattern (2D string matching, edits, and alignments)
1. Pattern 1: 0/1 Knapsack Problem
The Problem: Given $N$ items with weights weights[i] and values values[i], and a knapsack of maximum capacity $W$. Find the maximum total value we can pack into the knapsack such that each item can be chosen at most once (0 or 1).
State & Recurrence
Let dp[i][w] be the maximum value achievable using a subset of the first i items with a remaining weight capacity w.
For each item $i$:
- 1Exclude item $i$:
dp[i-1][w] - 2Include item $i$ (if $weights[i-1] \le w$):
values[i-1] + dp[i-1][w - weights[i-1]]
$$\text{dp}[i][w] = \max(\text{dp}[i-1][w], \text{values}[i-1] + \text{dp}[i-1][w - \text{weights}[i-1]])$$
1D Space Optimization (The Reverse Loop Trick)
Notice that state dp[w] depends solely on values from the previous row at or to the left of w. By traversing the capacity backwards from $W$ down to $\text{weight}$, we can compress the 2D grid into a single 1D array without overwriting needed prior states:
2. Pattern 2: Coin Change (Unbounded Knapsack)
The Problem (LeetCode 322): Given coins of different denominations and a total amount of money, return the minimum number of coins needed to make up that amount. If that amount cannot be made up, return -1. Each coin is available in unlimited supply.
State & Recurrence
Let dp[a] be the minimum coins needed to produce amount a.
$$\text{dp}[a] = \min_{c \in \text{coins}, c \le a} (\text{dp}[a - c] + 1)$$
Base case: dp[0] = 0; all other amounts initialized to $\infty$.
Step-by-Step Trace: Coins [1, 2, 5], Target 11
| Amount ($a$) | Calculation | dp[a] | Optimal Combination |
|---|---|---|---|
| 0 | Base Case | 0 | [] |
| 1 | $\text{dp}[1-1]+1 = 0+1$ | 1 | [1] |
| 2 | $\min(\text{dp}[1]+1, \text{dp}[0]+1) = \min(2, 1)$ | 1 | [2] |
| 5 | $\min(\text{dp}[4]+1, \text{dp}[3]+1, \text{dp}[0]+1)$ | 1 | [5] |
| 11 | $\min(\text{dp}[10]+1, \text{dp}[9]+1, \text{dp}[6]+1) = 2+1$ | 3 | [5, 5, 1] |
3. Pattern 3: Longest Common Subsequence (LCS)
The Problem (LeetCode 1143): Given two strings text1 and text2, return the length of their longest common subsequence. (A subsequence retains relative order without requiring consecutive adjacency).
State & 2D Recurrence
Let dp[i][j] be the LCS of text1[0...i-1] and text2[0...j-1].
$$\text{dp}[i][j] = \begin{cases} 1 + \text{dp}[i-1][j-1] & \text{if } text1[i-1] == text2[j-1] \\ \max(\text{dp}[i-1][j], \text{dp}[i][j-1]) & \text{if } text1[i-1] \neq text2[j-1] \end{cases}$$
4. Dual-Language Implementation
Python 3 Implementation: All 3 Master Patterns
Modern JavaScript (Node.js) Implementation
5. Pattern Recognition Cheat Sheet
| LeetCode Problem | Archetype Pattern | Time Complexity | Space Complexity |
|---|---|---|---|
| Partition Equal Subset Sum (416) | 0/1 Knapsack | $O(N \times \text{Target})$ | $O(\text{Target})$ |
| Target Sum (494) | 0/1 Knapsack | $O(N \times \text{Sum})$ | $O(\text{Sum})$ |
| Coin Change II (518) | Unbounded Knapsack (Forward Loop) | $O(N \times \text{Amount})$ | $O(\text{Amount})$ |
| Edit Distance (72) | 2D LCS Variant | $O(M \times N)$ | $O(M \times N)$ |
| Longest Palindromic Subsequence (516) | LCS of String with its Reverse | $O(N^2)$ | $O(N^2)$ |
Multiple Choice Questions
text1[i-1] == text2[j-1]?Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Dynamic Programming: Overlapping Subproblems, Memoization vs Tabulation | None |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.