Dynamic Programming21 sections · 916 units
Open in Course

0/1 Knapsack - Problem Statement

The classic

You have nn items. Each item ii has a weight wiw_i (how much space it takes) and a value viv_i (how much it's worth). You have a bag that holds at most WW total weight.

Your goal: pick items to get the highest total value without exceeding the weight limit. The catch? Each item can only be taken once (that's the "0/1" part, you either take it or you don't). This problem has a clean structure that makes it perfect for DP. Before I show you the solution, try thinking: why can't you just grab the most valuable items first?