FastPrepMinimum Bus Fleet Across Stations

Minimum Bus Fleet Across Stations

Snowflake logoSnowflakeMediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each trip in trips is [origin, destination, departure, arrival], where the two times are decimal nonnegative integers.

A bus can operate a trip when it is initially assigned to that trip's origin or when it completed an earlier trip at the same origin at or before the new departure time. A bus cannot reposition between different stations without a listed trip.

Return the minimum number of buses required to operate every trip.

Function

minimumBuses(trips: String[][]) → int

Examples

Example 1

trips = [["A","B","0","5"],["B","C","5","9"],["A","C","2","6"]]return = 2

One bus continues from the first trip to the second; another starts the overlapping A-to-C trip.

Example 2

trips = [["A","B","1","4"],["C","A","0","1"],["B","C","4","8"]]return = 1

A single bus can operate C-to-A, A-to-B, then B-to-C.

Example 3

trips = [["A","B","0","10"],["A","C","1","2"],["B","A","10","12"]]return = 2

The two early departures from A overlap.

Constraints

  • 1 <= trips.length <= 10^5.
  • 0 <= departure < arrival <= 10^9.
  • Station names are non-empty ASCII strings.

More Snowflake problems

See Snowflake hiring insights
public int minimumBuses(String[][] trips) {
    // Write your solution here.
}
trips[["A","B","0","5"],["B","C","5","9"],["A","C","2","6"]]
expected2
Checking account…