Maximize the Eligible Carpool Group
Each rider i accepts a carpool only when the number of other selected riders lies in the inclusive interval [lower[i], upper[i]]. Choose any subset of riders and return the largest possible group size.
Function Signature
max_eligible_riders(lower: list[int], upper: list[int]) -> int
Valid Input Domain
The arrays have equal nonzero length N. Every interval is valid and bounded between zero and N minus one.
Exact Output Semantics
Return the maximum integer k for which at least k riders satisfy lower[i] <= k - 1 <= upper[i]. Return only k; the selected subset is not required, so ties among subsets do not affect the output.
Constraints
-
1 <= N <= 200,000.
-
0 <= lower[i] <= upper[i] <= N - 1.
-
The required target time complexity is O(N).
Public Examples
Example 1
Input: lower = [0, 1, 1, 2, 2], upper = [1, 2, 2, 4, 4]
Output: 3
At least three riders accept traveling with two other riders.
Example 2
Input: lower = [0, 0, 0], upper = [0, 1, 2]
Output: 2
Two riders accept a group with one other rider, but fewer than three accept a group with two others.
Hints
-
For a proposed group size, only the number of intervals covering k minus one matters.
-
Use the bounded interval endpoints to avoid testing every rider against every possible k.