0/1 knapsack

medium

Leaving an item inherits the row above; taking it looks back at reduced capacity and adds the value. The arrows show which, and following them back recovers the bag's contents.

Worst O(n × W)Space O(n × W), or O(W) with the 1-D trick
Loading the 0/1 knapsack engine…

Where to go next