Problem · Array

Maximize Compatible Car Riders

Learn this problem
MediumUber Freight logoUber FreightFULLTIMEPHONE SCREEN

Problem 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[]) → int

Examples

Example 1

minCoRiders = [0,1,1,2,2]maxCoRiders = [1,2,2,4,4]return = 3

A 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 = 2

Two riders accept one co-rider each; a group of three has only two eligible riders.

Constraints

  • 1 <= minCoRiders.length <= 200000
  • maxCoRiders.length == minCoRiders.length
  • 0 <= minCoRiders[i] <= maxCoRiders[i] < n
drafts saved locally
public int maximumCompatibleRiders(int[] minCoRiders, int[] maxCoRiders) {
    // Write your code here.
}
minCoRiders[0,1,1,2,2]
maxCoRiders[1,2,2,4,4]
expected3
checking account