FastPrepReach a Meeting by Scheduled Trains

Reach a Meeting by Scheduled Trains

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

A timetable contains directed train rides. Ride i leaves city origins[i] at time departures[i], reaches city destinations[i] at time arrivals[i], and may be used at most once.

You begin in start at startTime. You may wait in any city for any nonnegative amount of time. You may board a ride when you are in its origin city at or before its departure time.

Return true if you can reach destination no later than meetingTime. Otherwise, return false.

A transfer is allowed when one ride arrives at the exact time that the next ride departs. If start already equals destination, reaching the meeting depends only on whether startTime is no later than meetingTime.

Function

canReachMeeting(origins: String[], departures: int[], destinations: String[], arrivals: int[], start: String, startTime: int, destination: String, meetingTime: int) → boolean

Examples

Example 1

origins = ["A","B","A"]departures = [1,5,3]destinations = ["B","C","C"]arrivals = [4,7,10]start = "A"startTime = 0destination = "C"meetingTime = 8return = true

Take the ride from A to B, arriving at 4, then take the ride from B to C, arriving at 7.

Example 2

origins = ["A","B","A"]departures = [1,5,3]destinations = ["B","C","C"]arrivals = [4,7,10]start = "A"startTime = 0destination = "C"meetingTime = 6return = false

The earliest possible arrival in C is time 7, which is after the meeting time.

Example 3

origins = ["A","B"]departures = [2,5]destinations = ["B","C"]arrivals = [5,6]start = "A"startTime = 2destination = "C"meetingTime = 6return = true

You board the first ride exactly at startTime. It arrives in B at time 5, allowing an exact-time transfer to the second ride.

Constraints

  • 1 <= origins.length <= 2 * 10^5.
  • origins.length == departures.length == destinations.length == arrivals.length.
  • Every city name is nonempty and contains at most 30 letters or digits.
  • 0 <= departures[i] <= arrivals[i] <= 10^9.
  • 0 <= startTime, meetingTime <= 10^9.

More Google problems

See Google hiring insights
public boolean canReachMeeting(String[] origins, int[] departures, String[] destinations, int[] arrivals, String start, int startTime, String destination, int meetingTime) {
  // Write your code here.
}
origins["A","B","A"]
departures[1,5,3]
destinations["B","C","C"]
arrivals[4,7,10]
start"A"
startTime0
destination"C"
meetingTime8
expectedtrue
Checking account…