0/1 Knapsack. Compare recursion, memoisation, a 2D table and the 1D array iterated backwards.
Problem
Items with weights and values, capacity W; each item can be taken at most once. Maximise total value.
weights = [1, 3, 4, 5], values = [1, 4, 5, 7], W = 7 → 9 (items with weights 3 and 4).
Pattern
Take-or-skip DP: dp[i][w] = best value using the first i items within capacity w = max(skip: dp[i-1][w], take: dp[i-1][w - wt] + val).
Approach 1: Recursion over take/skip (feasible, O(2ⁿ))
class KnapsackRecursive {
static int best(int[] wt, int[] val, int cap) { return go(wt, val, wt.length, cap); }
private static int go(int[] wt, int[] val, int i, int cap) {
if (i == 0 || cap == 0) return 0;
int skip = go(wt, val, i - 1, cap);
return wt[i - 1] > cap ? skip : Math.max(skip, val[i - 1] + go(wt, val, i - 1, cap - wt[i - 1]));
}
}Approach 2: Memoisation (O(n·W))
import java.util.HashMap;
import java.util.Map;
class KnapsackMemo {
static int best(int[] wt, int[] val, int cap) { return go(wt, val, wt.length, cap, new HashMap<>()); }
private static int go(int[] wt, int[] val, int i, int cap, Map<Long, Integer> memo) {
if (i == 0 || cap == 0) return 0;
long key = ((long) i << 32) | cap;
Integer cached = memo.get(key);
if (cached != null) return cached;
int skip = go(wt, val, i - 1, cap, memo);
int r = wt[i - 1] > cap ? skip : Math.max(skip, val[i - 1] + go(wt, val, i - 1, cap - wt[i - 1], memo));
memo.put(key, r);
return r;
}
}Approach 3: 2D table (O(n·W) time and space)
class KnapsackTable {
static int best(int[] wt, int[] val, int cap) {
int n = wt.length;
int[][] dp = new int[n + 1][cap + 1];
for (int i = 1; i <= n; i++)
for (int w = 0; w <= cap; w++) {
dp[i][w] = dp[i - 1][w];
if (wt[i - 1] <= w) dp[i][w] = Math.max(dp[i][w], dp[i - 1][w - wt[i - 1]] + val[i - 1]);
}
return dp[n][cap];
}
}Approach 4: One array, capacity iterated backwards (O(n·W) time, O(W) space) — optimal
Going from high to low capacity ensures each item is used at most once (going forwards would allow reuse, which is the unbounded knapsack).
class KnapsackOneRow {
static int best(int[] wt, int[] val, int cap) {
int[] dp = new int[cap + 1];
for (int i = 0; i < wt.length; i++)
for (int w = cap; w >= wt[i]; w--)
dp[w] = Math.max(dp[w], dp[w - wt[i]] + val[i]);
return dp[cap];
}
}
class KnapsackDemo {
public static void main(String[] args) {
int[] wt = {1, 3, 4, 5}, val = {1, 4, 5, 7};
System.out.println(KnapsackRecursive.best(wt, val, 7) + " " + KnapsackMemo.best(wt, val, 7) + " "
+ KnapsackTable.best(wt, val, 7) + " " + KnapsackOneRow.best(wt, val, 7)); // 9 9 9 9
}
}Feasible → optimal
O(2ⁿ) → O(n·W) with memoisation or a table → O(n·W) time and O(W) space with one backwards row. Note O(n·W) is pseudo-polynomial: it depends on the numeric capacity.
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): partition equal subset sum.
Is there a subset with sum total / 2? A boolean knapsack: can[s] |= can[s - x], iterating s backwards for each x. O(n × sum). BitSet shifting (bits.or(bits << x)) makes it very fast in practice.
Related (same pattern): unbounded knapsack and coin change.
When each item can be used many times, iterate capacity forwards in the 1D array. Coin change (minimum coins) and "number of ways to make change" are the same structure (see the Coin Change question).