Skip to content

Knapsack & Interval DP

Two classic DP shapes: choose items under a capacity, or solve a growing interval

1. Choices and state

In 0/1 knapsack, each item has a weight and a value. You may take each item once, and the total weight must fit in a capacity W. The question is the maximum total value. The hard part is not the loop; it is choosing a state that remembers exactly what the future needs: the items considered so far and the capacity still available.

State

The state is two-dimensional because both the item index and the remaining capacity matter. If you forget either one, the table cannot distinguish two different situations that need different answers.

2. The knapsack recurrence

For item i, you have two choices. Skip it and keep the answer from the previous row, or take it and add its value to the cell at capacity c − weight. The larger of the two wins. If the item does not fit, only the skip choice is available.

0/1 knapsack
Bottom-up
for i in 1..n:
  for c in 0..W:
    dp[i][c] = dp[i-1][c]
    if weight[i] <= c:
      dp[i][c] = max(dp[i][c], dp[i-1][c-weight[i]] + value[i])
return dp[n][W]

Example: A(2,3), B(3,4), C(4,5), capacity 5

Row A can take A for capacities 2..5, value 3. Row B improves capacity 5 to 7 by taking A and B (weight 5). Row C cannot beat 7: C alone is 5, and C plus A needs capacity 6. The answer is 7, with items A and B.

3. Recover the items

The table stores values, not choices. To recover the subset, walk backward from dp[n][W]. If the value differs from the row above, item n was taken; subtract its weight and move up. Otherwise, move up without taking it. The walk visits one cell per item, so it is O(n).

Backtracking
c = W
for i from n down to 1:
  if dp[i][c] != dp[i-1][c]:
    take item i
    c -= weight[i]

4. Interval DP

Interval DP solves problems on a contiguous range. The state is a pair of endpoints dp[i][j], and the recurrence splits the interval or compares its ends. The table is filled by increasing interval length, because a longer interval depends on shorter ones inside it.

Longest palindromic subsequence

For “bbabcbcab”, the longest palindromic subsequence has length 7. The subsequence does not have to be contiguous; that is what separates it from the longest palindromic substring.

Other interval problems

  • Matrix-chain multiplication — choose where to split the chain.
  • Burst balloons — choose the last balloon to burst in an interval.
  • Optimal binary search tree — choose the root of each interval.
  • Stone merging — split a sequence into two contiguous parts.

5. Complexity

Knapsack is O(nW) time and O(nW) space. The space can be reduced to O(W) by keeping one row and iterating capacity downward, so each item is used at most once. Interval DP is usually O(n²) states with O(1) or O(n) work per state.

Knapsack

O(nW) is pseudo-polynomial: it depends on the numeric capacity, not just the number of items. A capacity of 10⁹ is not tractable with this table even if n is small.

6. Practice

Try it

Why does the knapsack recurrence use dp[i−1][c−w] instead of dp[i][c−w]?

Try it

Items A(2,3), B(3,4), capacity 5. What is the best value?

Try it

Why must interval DP fill by increasing length?

Try it

What is the difference between a palindromic substring and a palindromic subsequence?

7. Resources

The knapsack playground lets you change the capacity and regenerate items. Watch how one item changes a whole column, and backtrack from the bottom-right cell to see the chosen set.

Open the knapsack playground →

Continue the track

Open the playground for this topic

Knapsack Playground →

Change the capacity or the items and watch the 0/1 knapsack table compute the best value.

Site soundtrack