Skip to content

Instantly share code, notes, and snippets.

@stephenmathieson
Created November 28, 2018 19:17
Show Gist options
  • Select an option

  • Save stephenmathieson/c3d4cd77f6c6e55ab312224e0dac8f01 to your computer and use it in GitHub Desktop.

Select an option

Save stephenmathieson/c3d4cd77f6c6e55ab312224e0dac8f01 to your computer and use it in GitHub Desktop.

Revisions

  1. stephenmathieson created this gist Nov 28, 2018.
    33 changes: 33 additions & 0 deletions bucket-of-rocks.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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));