Fewest Drivers to Cover Every Trip Without Overlap
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Find the fewest drivers needed to serve all trips given [start, end) times, which reduces to the peak number of simultaneous trips and tests sweep-line or heap handling of boundaries.
A ride-hailing platform has a list of scheduled trips. Each trip is a pair [start, end] of integer timestamps. A driver can work on only one trip at a time, and every trip must be handled by exactly one driver.
Return the minimum number of drivers needed to serve all trips so that no driver is on two trips at the same moment.
Rules
- A trip occupies the half-open time span
[start, end). The driver is busy fromstartup to, but not including,end. - If one trip ends at time
tand another starts at timet, the same driver can take both. For example,[1, 5]and[5, 8]need only one driver. - A trip with
start == endoccupies no time, so it never forces an extra driver and contributes nothing to the answer. - The trips may appear in any order.
- If there are no trips, return
0.
Input
trips: a list of[start, end]pairs of integers, withstart <= end.
Output
- An integer: the smallest number of drivers that can cover all trips.
Constraints
0 <= len(trips) <= 10^5- Each trip has exactly two integers,
startandend, withstart <= end. - Timestamps are integers and may be any size that fits in a standard integer.
- The list is not guaranteed to be sorted.
Examples
Example 1
Input: min_drivers_needed([[1, 4], [2, 5], [5, 7]])
Output: 2
Example 2
Input: min_drivers_needed([[1, 3], [3, 6], [2, 4]])
Output: 2
Example 3
Input: min_drivers_needed([])
Output: 0
Hints
Hint 1
Each driver handles one trip at a time, so the answer can never be lower than the largest number of trips happening at the same instant. Think about whether that number is also enough.
Hint 2
Turn every trip into two events, one when it begins and one when it finishes, then process events in time order while keeping a running count of active trips.
Hint 3
Decide what happens when a finish and a start share the same timestamp. Under the half-open rule the finish must be handled first, which falls out of sorting (time, delta) pairs with -1 for finishes and +1 for starts.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The answer equals the maximum number of trips that are active at the same time.
- Lower bound: if
ktrips are active at one instant, no driver can serve two of them, so at leastkdrivers are required. - Achievability: handle trips in order of start time and give each one to any driver who is already free. A new driver is hired only when every existing driver is busy. At that moment all of them, plus the new trip, are active together, so the number of drivers never exceeds the peak overlap.
Since the lower bound and the upper bound match, the peak overlap is the answer.
Sweep line
- For each trip, create a
(start, +1)event and an(end, -1)event. - Sort all events. Tuples compare by time first, then by delta, so at equal times
-1comes before+1. This implements the half-open convention: a driver who finishes attcan immediately start a trip att. - Walk through the events, maintaining a running count of active trips, and record the largest value reached.
Edge cases
- Empty list: no events, the result is
0. - Back-to-back trips (
end == start): the end is processed first, so one driver suffices. - Identical trips:
kcopies needkdrivers. - Zero-length trips: the
-1is processed before the+1, so the count dips and recovers, and the peak is never raised. Such a trip needs no driver, matching the rule[t, t)is empty. - Unsorted input is fine because the algorithm sorts internally.
Complexity
- Time:
O(N log N), dominated by sorting the2Nevents. - Space:
O(N)for the event list.
An equivalent approach sorts trips by start and keeps a min-heap of end times, popping the earliest end when it is <= the next start. It has the same complexity and is handy if you also need to know which driver gets which trip.
Reference solution
from typing import List
def min_drivers_needed(trips: List[List[int]]) -> int:
# One +1 event at each start and one -1 event at each end.
events = []
for start, end in trips:
events.append((start, 1))
events.append((end, -1))
# Sorting tuples puts (t, -1) before (t, +1), so a trip ending at t
# releases its driver before a trip starting at t needs one.
events.sort()
active = 0
best = 0
for _, delta in events:
active += delta
if active > best:
best = active
return best