Created
June 6, 2017 15:58
-
-
Save Woodsphreaker/0caeedc8ba762ab51395e4eefc6fddd2 to your computer and use it in GitHub Desktop.
Given a number of people and an array of integers which are umbrellas people capacity, return the min number umbrellas needed
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 sort = (list) => list.sort((a,b) => a < b) | |
const minus = (a) => (b) => a - b | |
const size = (list) => list.length | |
const obelus = (a) => (b) => Math.floor(a / b) | |
const times = (a) => (b) => a * b | |
const calc = (a) => (b) => minus(a)(times(b)(obelus(a)(b))) | |
const solve = (peoples) => (umbrellas) => { | |
return sort(umbrellas).reduce((acc, cur, i) => { | |
acc[1] += obelus(acc[0])(cur) | |
acc[0] = calc(acc[0])(cur) | |
if ((size(umbrellas) - 1) === i && acc[0] !== 0) acc[1] = -1 | |
return acc | |
}, [peoples, 0]) | |
} | |
const tests = | |
[ { n: 3 | |
, umbrellas: [1, 2] | |
, expect: 2 | |
} | |
, { n: 10 | |
, umbrellas: [3, 1] | |
, expect: 4 | |
} | |
, { n: 3 | |
, umbrellas: [2] | |
, expect: -1 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment