Portions for the diners
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- lists
- sorting
- loops
Problem
"Nobody leaves this kitchen hungry," says chef Valeria, eyeing the bar. A diner is satisfied by a portion at least as big as their hunger, and each portion goes whole to a single person. Valeria wastes nothing: a light eater never gets the big portion someone else needs.
Write a function that takes hunger and portions, two lists of positive integers (either may be empty, and their lengths may differ), and returns the largest number of diners who can be satisfied with one portion each. A portion equal to the hunger is enough; a smaller one is no use to that diner, but may still suit another.
With [3, 1, 4] and [2, 5, 1, 3]: the 1 gets the 1, the 3 gets the 3 and the 4 gets the 5. That makes 3, and the 2 is left over. If either list is empty, 0.
Examples
The example
[3, 1, 4], [2, 5, 1, 3] → 3
More diners than portions
[5, 2, 8, 3], [4, 6] → 2
Small portions stay on the bar
[7, 7], [1, 2, 3, 8, 9] → 2
None is big enough
[10, 12], [3, 9] → 0
Exactly the right size
[4, 4, 4], [4, 4] → 2
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def fed_count(hunger, portions):
passJavaScript
function fedCount(hunger, portions) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.