Skip to content

Instantly share code, notes, and snippets.

@leetcode-notes
Forked from lubien/A-umbrella.md
Created August 28, 2020 21:00
Show Gist options
  • Save leetcode-notes/4f4712110618a362e3de78edacff7b57 to your computer and use it in GitHub Desktop.
Save leetcode-notes/4f4712110618a362e3de78edacff7b57 to your computer and use it in GitHub Desktop.
Umbrella Challenge

Umbrella Challenge

Given a number of people N and an array of integers, each one representing the amount of people a type of umbrella can handle, output the minimum number of umbrellas needed to handle N people.

No umbrella could have left spaces. Which means if a umbrella can handle 2 people, there should be 2 people under it.

If there's no solution, return -1.

Examples

solve(3, [1, 2]) means that we have 3 people and two kinds of umbrellas, one that hanled one person and one that handles 2. We can give one two-sized umbrella to 2 of them and the other to the last person. Therefore the solution is 2 (umbrellas). You could give 3 one-sized umbrellas, but we want the minimum number.

solve(10, [3, 1]). You can give 3 three-sized umbrellas and 1 one-sized. This means the solution is 4.

solve(3, [2]). There's no solution since one umbrella would have empty space. Return -1.

function solve(n, umbrellas) {
return -1
}
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