Train Schedule Reachability

Asked at GooglePhone screen

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

  1. Example 1

    Input: legs = [["A","Y",1,20],["A","Y",2,5],["Y","B",6,9]], source = A, destination = B

    Output: true

    Explanation: The leg arriving at Y at time 20 is too late to catch the Y to B train. Taking the leg that arrives at Y at time 5 allows boarding the leg departing at 6, reaching B.

  2. Example 2

    Input: legs = [["A","B",1,2]], source = A, destination = A

    Output: true

    Explanation: The start station and the destination are both A, so the trip is already complete without boarding any leg. The result is true.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.