Created
September 19, 2017 17:21
-
-
Save adrifmonte/ee0b99df1ddb567860c5a6638e2e1a7d to your computer and use it in GitHub Desktop.
Umbrella Challenge: https://gist.github.com/lubien/1f09a53a4b5607377166c58a7eb49ae0
This file contains 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
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