There are men gathered outside of a cinema hoping to watch the newly released movie Star Wars. The person will only go in if at least other people will go with him because he doesn't want to get lonely. Additionally, that person doesn't want to go with more than other people since it would get crowded and ruin the experience.
This text contains spaced integers each representing the of the individual. What is the maximum number of men that can get inside at one time such that no constraint is violated?
Details and assumptions:
1 2 3 4 5 |
|
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Idea: A third party calls out the number of people who will go. Everyone raises their hand if that number agrees with their personal constraints. If the number of hands agree with the number called out, all the people raising their hands go out. It is evident the referee has to call out the numbers in descending order.
This takes quiet a while to run. The time complexity is O ( n max ( H i ) )
Sorting the pairs by the L i and then by their H i values and using binary search to find the number of people who'd agree would reduce the time complexity to O ( lo g n max ( H i ) )