Train Schedule Reachability
Asked at Google
Reported by the problem's creator, not verified
A timetable is a list of legs. Each leg is [start, end, depart, arrive]: a train from station start to station end that departs at time depart and arrives at time arrive, with depart < arrive. Each leg can be taken at most once, at its stated times. You start at station source at time 0. After arriving somewhere at time t, you can board any leg from that station whose depart is at least t, so a leg leaving the very minute you arrive is allowed.
Examples
Example 1
Input: legs =
[["A","Y",1,20],["A","Y",2,5],["Y","B",6,9]], source =A, destination =BOutput:
trueExplanation: The leg arriving at
Yat time 20 is too late to catch theYtoBtrain. Taking the leg that arrives atYat time 5 allows boarding the leg departing at 6, reachingB.Example 2
Input: legs =
[["A","B",1,2]], source =A, destination =AOutput:
trueExplanation: The start station and the destination are both
A, so the trip is already complete without boarding any leg. The result istrue.