|
2 | 2 |
|
3 | 3 | ## General
|
4 | 4 |
|
5 |
| -- A good starting point is a brute-force recursive solution. |
6 |
| -You can see it sketched out in the first half of the article ["Demystifying the 0-1 knapsack problem: top solutions explained"](demystifying-the-knapsack-problem). |
7 |
| -- For a more efficient solution, you can improve your recursive solution using _dynamic programming_, which is introduced in the second half of [the above-mentioned article](demystifying-the-knapsack-problem). |
8 |
| -For a more general explainer, see the video ["5 Simple Steps for Solving Dynamic Programming Problems"](solving-dynamic-programming-problems) |
9 |
| -- If you need a more visual walkthrough of how to apply dynamic programming to the knapsack problem, see the video ["0/1 Knapsack problem | Dynamic Programming"](0-1-knapsack-problem). |
10 |
| -Also worth mentioning is [this answer](intuition-of-dp-for-knapsack-problem) to a question on Reddit, _"What is the intuition behind Knapsack problem solution using dynamic programming?"_. |
11 |
| -Below is the answer in full. |
| 5 | +- If you're not sure where to start, try a brute-force solution: |
| 6 | + - First, generate all possible combinations of items. [`Array#combination`](https://rubyapi.org/3.3/o/array#method-i-combination) might come in handy. |
| 7 | + - Then, find the combination that has the highest value and is within the weight limit. |
| 8 | +- If you want to make your solution as efficient as possible, look into an algorithmic technique called _dynamic programming_. Here are some resources: |
| 9 | + - ["Demystifying the 0-1 knapsack problem: top solutions explained"](demystifying-the-knapsack-problem). |
| 10 | + - ["5 Simple Steps for Solving Dynamic Programming Problems"](solving-dynamic-programming-problems) |
| 11 | + - ["0/1 Knapsack problem | Dynamic Programming"](0-1-knapsack-problem). |
| 12 | + - [This answer](intuition-of-dp-for-knapsack-problem) to a question on Reddit, _"What is the intuition behind Knapsack problem solution using dynamic programming?"_. Below is the answer in full. |
12 | 13 |
|
13 | 14 | > The intuition behind the solution is basically like any dynamic programming solution: split the task into many sub-tasks, and save solutions to these sub-tasks for later use.
|
14 | 15 | >
|
|
0 commit comments