Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What did you try? </p> <p>The idea, given the problem you stated (which specifies we must use recursion) is simple: for each item that you can take, see if it's better to take it or not. So there are only two possible path:</p> <ol> <li>you take the item</li> <li>you don't take it</li> </ol> <p>When you take the item, you remove it from your list and you decrease the capacity by the weight of the item.</p> <p>When you don't take the item, you remove if from you list but you do not decrease the capacity.</p> <p>Sometimes it helps to print what the recursive calls may look like. In this case, it could look like this:</p> <pre><code>Calling 11 8 7 6 5 with cap: 20 +Calling 8 7 6 5 with cap: 20 | Calling 7 6 5 with cap: 20 | Calling 6 5 with cap: 20 | Calling 5 with cap: 20 | Result: 5 | Calling 5 with cap: 14 | Result: 5 | Result: 11 | Calling 6 5 with cap: 13 | Calling 5 with cap: 13 | Result: 5 | Calling 5 with cap: 7 | Result: 5 | Result: 11 | Result: 18 | Calling 7 6 5 with cap: 12 | Calling 6 5 with cap: 12 | Calling 5 with cap: 12 | Result: 5 | Calling 5 with cap: 6 | Result: 5 | Result: 11 | Calling 6 5 with cap: 5 | Calling 5 with cap: 5 | Result: 5 | Result: 5 | Result: 12 +Result: 20 Calling 8 7 6 5 with cap: 9 Calling 7 6 5 with cap: 9 Calling 6 5 with cap: 9 Calling 5 with cap: 9 Result: 5 Calling 5 with cap: 3 Result: 0 Result: 6 Calling 6 5 with cap: 2 Calling 5 with cap: 2 Result: 0 Result: 0 Result: 7 Calling 7 6 5 with cap: 1 Calling 6 5 with cap: 1 Calling 5 with cap: 1 Result: 0 Result: 0 Result: 0 Result: 8 Result: 20 </code></pre> <p>I did on purpose show the call to [8 7 6 5] with a capacity of 20, which gives a result of 20 (8 + 7 + 5).</p> <p>Note that [8 7 6 5] is called twice: once with a capacity of 20 (because we didn't take 11) and once with a capacity of 9 (because with did take 11).</p> <p>So the path to the solution:</p> <p>11 not taken, calling [8 7 6 5] with a capacity of 20</p> <p>8 taken, calling [7 6 5] with a capacity of 12 (20 - 8)</p> <p>7 taken, calling [6 5] with a capacity of 5 (12 - 7)</p> <p>6 not taken, calling [5] with a capacity of 5</p> <p>5 taken, we're at zero.</p> <p>The actual method in Java can fit in very few lines of code.</p> <p>Since this is obviously homework, I'll just help you with a skeleton:</p> <pre><code>private int ukp( final int[] ar, final int cap ) { if ( ar.length == 1 ) { return ar[0] &lt;= cap ? ar[0] : 0; } else { final int[] nar = new int[ar.length-1]; System.arraycopy(ar, 1, nar, 0, nar.length); fint int item = ar[0]; if ( item &lt; cap ) { final int left = ... // fill me: we're not taking the item final int took = ... // fill me: we're taking the item return Math.max(took,left); } else { return ... // fill me: we're not taking the item } } } </code></pre> <p>I did copy the array to a new array, which is less efficient (but anyway recursion is not the way to go here if you seek performance), but more "functional".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload