Skip to content

Instantly share code, notes, and snippets.

@stephenmathieson
Created November 28, 2018 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenmathieson/c3d4cd77f6c6e55ab312224e0dac8f01 to your computer and use it in GitHub Desktop.
Save stephenmathieson/c3d4cd77f6c6e55ab312224e0dac8f01 to your computer and use it in GitHub Desktop.
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