0-1 Knapsack
To showcase a dynamic programming solution, exploring the properties of the problem that make it solvable by this technique, we shall look at the 0-1 knapsack problem.
You are given weights and values of n items. You must put these items in a knapsack of capacity W to get the maximum value of the knapsack. You cannot break an item. You can either pick it or not pick it (hence the 0-1 property).
In other words, you are given two arrays, values[0...n-1] and weights[0...n-1], which represent values and weights associated with n items, respectively. You want to find the maximum value subset of values[] such that the sum of weights of the subset is smaller than or equal to W.
The first immediate solution to this problem is to consider ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access