Problem · Array
Maximize Compatible Car Riders
Learn this problemProblem statement
There are n riders. Rider i is willing to share a car only when the number of other selected riders is between minCoRiders[i] and maxCoRiders[i], inclusive.
You may select any subset of riders. Return the maximum possible number of selected riders such that every selected rider accepts the resulting group size. Return 0 when no non-empty compatible group exists.
For a group of size k, a rider is eligible exactly when minCoRiders[i] <= k - 1 <= maxCoRiders[i].
Function
maximumCompatibleRiders(minCoRiders: int[], maxCoRiders: int[]) → intExamples
Example 1
minCoRiders = [0,1,1,2,2]maxCoRiders = [1,2,2,4,4]return = 3A compatible group of three can be chosen, while fewer than four riders accept having three co-riders.
Example 2
minCoRiders = [0,0,0]maxCoRiders = [0,2,2]return = 2Two riders accept one co-rider each; a group of three has only two eligible riders.
Constraints
1 <= minCoRiders.length <= 200000maxCoRiders.length == minCoRiders.length0 <= minCoRiders[i] <= maxCoRiders[i] < n