Fastest Route in Rush Hour
A route that takes 20 minutes at 10:00 can take an hour at 08:30. A navigation app that ignores the time of day sends people straight into traffic jams. You are writing the core of a planner that knows when each road is congested.
The city has n intersections, numbered 0 to n - 1, joined by one-way roads. Road i is roads[i] = [from, to, base, rushStart, rushEnd, rushTime]. A car that enters the road at minute t takes:
rushTimeminutes whenrushStart <= t < rushEnd(the road's rush window);baseminutes otherwise.
Only the minute the car enters a road matters: entering one minute before the rush window takes base minutes even if the drive overlaps the window. A road with rushStart == rushEnd has no rush window. Drivers may wait at any intersection for as long as they like before entering the next road.
The driver leaves intersection source at minute departure. Return the earliest minute at which they can arrive at target, or -1 if no route reaches it.
Example
n = 4, departure = 480 (08:00), from 0 to 3, with roads [0,1,10,480,540,40], [1,3,10,0,0,10], [0,2,15,0,0,15] and [2,3,15,0,0,15].
The road from 0 to 1 is in its rush window until 09:00. Driving it now takes 40 minutes; waiting for 09:00 and then taking 10 arrives even later. Through 1 the car reaches 3 at minute 530. Through 2 it reaches 3 at 480 + 15 + 15 = 510, which is the answer.
[ "4", "0", "3", "480", "[[0,1,10,480,540,40],[1,3,10,0,0,10],[0,2,15,0,0,15],[2,3,15,0,0,15]]" ]
Explanation. The example: the road through 1 is in its rush window.
[ "4", "0", "3", "420", "[[0,1,10,480,540,40],[1,3,10,0,0,10],[0,2,15,0,0,15],[2,3,15,0,0,15]]" ]
Explanation. An hour earlier there is no rush, and the route through 1 wins.
[ "2", "0", "1", "150", "[[0,1,10,100,200,500]]" ]
Explanation. Waiting for the rush to end beats driving through it.
[ "3", "0", "2", "0", "[[0,1,5,0,0,5]]" ]
Explanation. The target cannot be reached.
[ "2", "1", "1", "77", "[[0,1,1,0,0,1]]" ]
Explanation. Already at the target.
[ "2", "0", "1", "199", "[[0,1,10,100,200,50]]" ]
Explanation. One minute before the window ends, waiting is better.
[ "5", "0", "4", "10", "[[0,1,5,0,100,60],[1,4,5,0,0,5],[0,2,15,0,0,15],[2,3,15,0,0,15],[3,4,15,0,0,15]]" ]
Explanation. The longer way round avoids the jam.
[ "2", "0", "1", "0", "[[1,0,3,0,0,3]]" ]
Explanation. Roads are one-way.
[ "2", "0", "1", "50", "[[0,1,30,0,0,30],[0,1,10,0,1000,100]]" ]
Explanation. Two roads between the same intersections.
[ "3", "0", "2", "55", "[[0,1,10,0,0,10],[1,2,10,60,120,100],[0,2,75,0,0,75]]" ]
Explanation. Arriving at the second road during its rush.
[ "6", "0", "5", "0", "[[0,1,4,0,10,20],[0,2,2,0,0,2],[2,1,1,0,0,1],[1,3,5,0,5,50],[2,4,10,0,0,10],[4,5,3,0,0,3],[3,5,2,0,0,2]]" ]
Explanation. Waiting at intersection 1 for two minutes is part of the fastest route.
Follow-up: Real traffic is not two-valued: a road's travel time changes every five minutes through the day. Which property must those travel-time profiles have for your algorithm to stay correct, and how do you restore it if the raw data breaks it?
- `2 <= n <= 10^4` - `0 <= roads.length <= 5 * 10^4`, and every road has 6 values. - `0 <= from, to, source, target < n`; there may be several roads between the same intersections. - `1 <= base <= rushTime <= 10^4` - `0 <= rushStart <= rushEnd <= 10^6` - `0 <= departure <= 10^6`
- Views
- 2