FastPrepEarliest Car for Each Passenger

Earliest Car for Each Passenger

WhatNot logoWhatNot● MediumFULLTIMENEW GRADPHONE SCREEN
Learn

Problem statement

Cars depart in the order given. Each car follows a fixed route represented by the attractions it visits in travel order. Each passenger supplies an ordered list of attractions they want to visit.

Assign each passenger to the earliest departing car whose route contains the passenger's entire requested list as a subsequence. Return the assigned zero-based car index for every passenger, or -1 when no route can serve that passenger.

Function

assignEarliestCars(carRoutes: int[][], passengerStops: int[][]) → int[]

Examples

Example 1

carRoutes = [[1,3,5,7],[1,2,3,4],[2,5,7]]passengerStops = [[1,5,7],[1,4],[5,2],[]]return = [0,1,-1,0]

The first two requests are served by cars 0 and 1. No route visits 5 before 2.

Example 2

carRoutes = [[4,8],[4,6,8],[8]]passengerStops = [[4,8],[8]]return = [0,0]

Car 0 is the earliest compatible route for both passengers.

Example 3

carRoutes = []passengerStops = [[],[1]]return = [-1,-1]

There is no car to assign.

Constraints

  • 0 <= carRoutes.length, passengerStops.length <= 500.
  • Each route and requested list contains at most 500 integer attraction IDs.
  • The total number of route and request entries is at most 100000.
  • A passenger with an empty request can take the first car, or receives -1 when there are no cars.

More WhatNot problems

See WhatNot hiring insights
public int[] assignEarliestCars(int[][] carRoutes, int[][] passengerStops) {
    // Write your solution here.
}
carRoutes[[1,3,5,7],[1,2,3,4],[2,5,7]]
passengerStops[[1,5,7],[1,4],[5,2],[]]
expected[0,1,-1,0]
Checking account…