Dynamic Programming21 sections · 916 units
Open in Course

Knapsack 2 - The Observation

Swap dimensions

Instead of asking "What's the max value for capacity ww?", ask "What's the min weight to achieve value vv?" If you can compute the minimum weight needed for every possible value from 00 to VmaxV_{max}, you can find the largest vv where that minimum weight is at most WW.

Same problem, different angle. The dimension that was 10910^9 becomes the answer you're searching for. The dimension that was at most 100,000100{,}000 becomes your DP index.