Created
November 28, 2018 19:17
-
-
Save stephenmathieson/c3d4cd77f6c6e55ab312224e0dac8f01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fillBucket(remainingWeight, piles, n) { | |
if (n === 0 || remainingWeight === 0) { | |
return 0; | |
} | |
const pile = piles[n - 1]; | |
if (pile.weight > remainingWeight) { | |
return fillBucket(remainingWeight, piles, n - 1); | |
} | |
return Math.max( | |
pile.count + fillBucket(remainingWeight - pile.weight, piles, n - 1), | |
fillBucket(remainingWeight, piles, n - 1) | |
); | |
} | |
const maxWeight = 50; | |
const piles = [ | |
{ | |
count: 60, | |
weight: 10 | |
}, | |
{ | |
count: 120, | |
weight: 30 | |
}, | |
{ | |
count: 100, | |
weight: 20 | |
} | |
]; | |
console.log(fillBucket(maxWeight, piles, piles.length)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment