Skip to content

Instantly share code, notes, and snippets.

@adrifmonte
Created September 19, 2017 17:21
Show Gist options
  • Save adrifmonte/ee0b99df1ddb567860c5a6638e2e1a7d to your computer and use it in GitHub Desktop.
Save adrifmonte/ee0b99df1ddb567860c5a6638e2e1a7d to your computer and use it in GitHub Desktop.
const solve = (people, umbrellas, result = 0) => {
if (!people) return result;
const bestUmbrellaToPick = umbrellas.reduce((best, umbrella) => {
return umbrella > best && umbrella <= people ? umbrella : best
}, 0);
if (!bestUmbrellaToPick) return -1;
const quantity = parseInt(people/bestUmbrellaToPick);
return solve(people - bestUmbrellaToPick*quantity, umbrellas, result + quantity);
}
const tests = [{
n: 3,
umbrellas: [1, 2],
expect: 2
}, {
n: 10,
umbrellas: [3, 1],
expect: 4
}, {
n: 3,
umbrellas: [2],
expect: -1
}];
tests.forEach(({ n, umbrellas, expect }) =>
console.log(`solve(${n}, [${umbrellas}])`) ||
console.log(` Expects: ${expect}`) ||
console.log(` Output: ${solve(n, umbrellas)}`) ||
console.log()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment