Quant Memo
Core

Knapsack Problems

Given a budget and a set of items each with a cost and a value, which items maximize value without exceeding the budget? The 0/1 and unbounded variants differ by one line of code and one big idea: can you reuse an item.

Prerequisites: Memoization vs Tabulation, Big-O Complexity

You have a fixed capital budget and a list of trade ideas, each with a required capital outlay and an expected payoff. Which subset of ideas maximizes total payoff without exceeding the budget? This is the knapsack problem, and it's a genuinely useful mental model for capital-constrained selection problems, not just a textbook exercise.

The idea: a table indexed by items considered and capacity used

In the 0/1 knapsack (each item taken at most once), define dp[i][c]dp[i][c] as the best achievable value using only the first ii items with capacity budget cc. For each item, you have exactly two choices: skip it, keeping dp[i1][c]dp[i-1][c], or take it (if it fits), gaining its value plus the best you could do with the remaining capacity using only earlier items — valuei+dp[i1][cweighti]\text{value}_i + dp[i-1][c - \text{weight}_i]. The recurrence takes the better of the two:

dp[i][c]=max(dp[i1][c],  valuei+dp[i1][cweighti])dp[i][c] = \max\big(dp[i-1][c],\ \ \text{value}_i + dp[i-1][c - \text{weight}_i]\big)

In words: either this item isn't worth its cost given what's left, or it is, and the recurrence checks both possibilities and keeps the winner. Time and space are O(nC)O(n \cdot C) where CC is the capacity — polynomial in the capacity's value, not its number of digits, which is why knapsack is only "efficient" for moderate budgets, not for a budget of, say, $10 billion in cent-sized units.

The unbounded knapsack (unlimited copies of each item — think a bond you can buy in unlimited size) drops the "i1i-1" on the take-branch, since reusing the same item is now allowed: dp[c]=max(dp[c], valuei+dp[cweighti])dp[c] = \max(dp[c],\ \text{value}_i + dp[c - \text{weight}_i]), iterating capacity forward instead of backward.

capacity c → item i-1item i dp[i][c] skip take
Each cell looks at only two earlier cells: directly above (skip this item) and diagonally back by the item's weight (take it), then keeps the max.

Worked example

Budget 5. Items: (weight 2, value 3), (weight 3, value 4), (weight 4, value 5). Building dp[i][c]dp[i][c] for c=0..5c=0..5: with only item 1 (w=2,v=3w{=}2,v{=}3), row is [0,0,3,3,3,3]. Adding item 2 (w=3,v=4w{=}3,v{=}4): at c=5c{=}5, skip gives 3 (previous row), take gives 4+dprow1[53=2]=4+3=74 + dp_{\text{row1}}[5-3=2] = 4+3=7 — take wins, so dp[2][5]=7dp[2][5]=7 (items 1 and 2, weight 2+3=52+3=5, value 3+4=73+4=7). Adding item 3 (w=4,v=5w{=}4,v{=}5) at c=5c{=}5: skip gives 7, take gives 5+dprow2[54=1]=5+0=55 + dp_{\text{row2}}[5-4=1]=5+0=5 — skip wins. Final answer: 7, using items 1 and 2.

def knapsack_01(weights, values, capacity):    # O(n * capacity)
    n = len(weights)
    dp = [0] * (capacity + 1)
    for i in range(n):
        for c in range(capacity, weights[i] - 1, -1):   # reverse: 0/1, no reuse
            dp[c] = max(dp[c], values[i] + dp[c - weights[i]])
    return dp[capacity]

0/1 knapsack: iterate capacity backward so each item is used at most once. Unbounded knapsack: iterate capacity forward so an item can feed off its own already-updated value, allowing reuse. That single loop direction is the entire difference between the two problems.

"Subset sum" and "partition into two equal-sum subsets" are 0/1 knapsack with value equal to weight — recognizing that reduction turns an unfamiliar problem into a familiar template instantly.

Where it shows up

Interview staples: partition equal subset sum, coin change (unbounded knapsack), target sum. On a desk, knapsack-style reasoning appears in capital-constrained trade selection — picking which positions to hold under a margin or capital budget to maximize expected P&L — and in bin-packing-adjacent problems like allocating a fixed risk budget across strategies with different capacity requirements and expected returns.

Related concepts

Practice in interviews

Further reading

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms (ch. 15, DP)
ShareTwitterLinkedIn