Minimum Bus Fleet

Asked at SnowflakePhone screen

Reported by the problem's creator, not verified

A bus company operates a list of trips, where each trip is [departure, arrival, origin, destination]: a bus leaves station origin at time departure and reaches station destination at time arrival.

Buses may start the day at any station. A bus can only change stations by driving a trip, so after a trip it waits at that trip's destination. A bus that arrives at a station at time t can start a trip from that station departing at time t or later. Every trip is driven exactly once, by one bus.

Examples

  1. Example 1

    Input: trips = [[0,4,1,2],[4,7,2,1],[4,6,1,2]]

    Output: 2

    Explanation: One bus drives the first trip, waits at station 2, and departs on the second trip at time 4. No bus is at station 1 when the third trip departs at time 4, so a second bus drives it.

  2. Example 2

    Input: trips = [[0,2,1,2],[3,5,1,2]]

    Output: 2

    Explanation: Both trips leave station 1. After the first trip the bus sits at station 2, where no trip departs, so the second trip needs its own bus.

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.