1800. Nearest Free Driver
A ride service runs drivers along a straight road. Driver i (its id is the index i) starts at position drivers[i]. Request j is placed at time times[j], wants a pickup at pickups[j] and a dropoff at dropoffs[j]. Requests are handled in increasing order of times, and equal times are handled in increasing index order.
Before a request is handled, every driver whose trip ends at or before its time is free again and stands at that trip's dropoff. The request goes to the free driver nearest to its pickup; ties go to the smaller current position, then the smaller id. That driver is busy until time + |position - pickup| + |pickup - dropoff|. With no free driver the answer is -1 and nothing changes.
Return an array with the driver id (or -1) for each request, in the original request order. Use 64-bit time values. Aim for O((D + R) log (D + R)) time.
Example 1
- Input:
- drivers = [1,10]times = [0,0,5]pickups = [2,9,3]dropoffs = [4,1,7]
- Output:
- [0,1,0]
- Explanation:
Request 0 takes driver 0 (distance 1) and keeps it busy until time 3, ending at position 4; request 1 takes driver 1; at time 5 only driver 0 is free again, standing at 4, so it serves request 2.
Example 2
- Input:
- drivers = [5]times = [0,1]pickups = [5,5]dropoffs = [8,8]
- Output:
- [0,-1]
- Explanation:
Driver 0 picks up at distance 0 and is busy until time 3, so the request at time 1 finds nobody free and gets -1.
Example 3
- Input:
- drivers = [3,7,3]times = [2]pickups = [5]dropoffs = [5]
- Output:
- [0]
- Explanation:
All three drivers are at distance 2 from the pickup; the tie goes to the smaller position 3, and between ids 0 and 2 the smaller id 0 wins.
Constraints
1 ≤ drivers.length ≤ 105; times, pickups and dropoffs share one length R with 1 ≤ R ≤ 105
0 ≤ times[j] ≤ 109 (the times need not be sorted); 0 ≤ drivers[i], pickups[j], dropoffs[j] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 3,400 msC++ 850 msJava 1,700 msJavaScript 1,700 msTypeScript 1,700 ms
Expected complexity
- Time
- O((D + R) log (D + R))
- Space
- O(D + R)