Skip to content

Instantly share code, notes, and snippets.

@anabastos
Last active May 28, 2021 18:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anabastos/6e142c6b9bf325cabe97fc3d16ef3e14 to your computer and use it in GitHub Desktop.
Save anabastos/6e142c6b9bf325cabe97fc3d16ef3e14 to your computer and use it in GitHub Desktop.
Umbrella.js
//Solution of https://gist.github.com/lubien/1f09a53a4b5607377166c58a7eb49ae0
const solve = (people, umbrellas) => {
const biggerUmbrella = Math.max(...umbrellas)
const remain = people % biggerUmbrella
const peopleThatFit = Math.floor(people / biggerUmbrella)
if (remain >= 1 && umbrellas.length === 1) {
return -1
} else {
const remainingUmbrellas = umbrellas.filter(umbrella => umbrella !== biggerUmbrella)
return remain !== 0 ? solve(remain, remainingUmbrellas) + peopleThatFit : peopleThatFit
}
}
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