1
1
/*
2
- * 0-1 Knapsack Problem: Imagine you are a thief and you want to steal things with room full of things.
2
+ * 0-1 Knapsack Problem: Imagine you are a thief and you want to steal things from room full of things.
3
3
* You have a knapsack which can handle maximum capacity of weight W, and you want to fill it up such
4
- * that it's worth is maximum. Being an intelligent thief, you know weights and values of each item in room.
5
- * How would you fill your knapsack, such that you get the maximum possible value.
4
+ * that it's worth is maximum. Being an intelligent thief, you know weights and values of each item in the room.
5
+ * How would you fill your knapsack, such that you get the maximum possible value for your knapsack of capacity W .
6
6
*
7
7
* The problem essentially boils down to whether item i would be part of your loot or not.
8
8
* This has optimal substructure:
14
14
* Then the maximum value of the loot is determined by remaining n-1 items whose possible weight would be
15
15
* W. (i.e excluding the ith item)
16
16
*
17
- * We could get all subsets of possible items in your loot, and choose the one with maximum value.
17
+ * We could get all subsets of possible items in the loot, and choose the one with maximum value.
18
18
*
19
19
* Since this problem will have overlapping subproblems, we could use dynamic programming to solve it.
20
20
* So that we don't recurse to calculate the same subproblem which we have already calculated.
@@ -64,4 +64,4 @@ int main()
64
64
std::cout << " Maximum possible loot value for capacity " << capacity
65
65
<< " : " << maximum_possible_loot_value (weights, values, capacity) << std::endl;
66
66
return 0 ;
67
- }
67
+ }
0 commit comments