|
| 1 | +# Instructions |
| 2 | + |
| 3 | +In this exercise, let's try to solve a classic problem. |
| 4 | + |
| 5 | +Bob is a thief. |
| 6 | +After months of careful planning, he finally manages to crack the security systems of a high-class apartment. |
| 7 | + |
| 8 | +In front of him are many items, each with a value (v) and weight (w). |
| 9 | +Bob, of course, wants to maximize the total value he can get; he would gladly take all of the items if he could. |
| 10 | +However, to his horror, he realizes that the knapsack he carries with him can only hold so much weight (W). |
| 11 | + |
| 12 | +Given a knapsack with a specific carrying capacity (W), help Bob determine the maximum value he can get from the items in the house. |
| 13 | +Note that Bob can take only one of each item. |
| 14 | + |
| 15 | +All values given will be strictly positive. |
| 16 | +Items will be represented as a list of pairs, `wi` and `vi`, where the first element `wi` is the weight of the *i*th item and `vi` is the value for that item. |
| 17 | + |
| 18 | +For example: |
| 19 | + |
| 20 | +Items: [ |
| 21 | + { "weight": 5, "value": 10 }, |
| 22 | + { "weight": 4, "value": 40 }, |
| 23 | + { "weight": 6, "value": 30 }, |
| 24 | + { "weight": 4, "value": 50 } |
| 25 | +] |
| 26 | + |
| 27 | +Knapsack Limit: 10 |
| 28 | + |
| 29 | +For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on. |
| 30 | + |
| 31 | +In this example, Bob should take the second and fourth item to maximize his value, which, in this case, is 90. |
| 32 | +He cannot get more than 90 as his knapsack has a weight limit of 10. |
0 commit comments