Quick Overview

Given scheduled trips with start and end times, return the fewest buses needed so that no bus runs two trips at once, where a bus may start a trip the moment its previous one ends. It tests reasoning about maximum interval overlap and boundary handling.

Fewest Buses Needed to Cover Every Scheduled Trip

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

A transit operator has a list of scheduled trips, each with a start time and an end time, and a bus can run only one trip at a time. Return the smallest number of buses that can cover every trip. ### Function Signature ```python def min_buses(trips: list[list[int]]) -> int: ``` ### Rules - `trips[i] = [start, end]` with `start < end`. The bus running trip `i` is busy from `start` up to, but not including, `end`. - A bus that finishes a trip at time `t` can start another trip at time `t`. - Any free bus can take any trip; travel between the end of one trip and the start of the next is not modeled. ### Constraints - `1 <= len(trips) <= 10^5` - `0 <= start < end <= 10^9` for every trip ### Examples **Example 1** ```text Input: trips = [[1, 4], [2, 5], [7, 9], [3, 6]] Output: 3 ``` At time 3, the trips `[1, 4]`, `[2, 5]` and `[3, 6]` are all running, so three buses are needed. **Example 2** ```text Input: trips = [[1, 3], [3, 5], [5, 8]] Output: 1 ``` Each trip starts exactly when the previous one ends, so one bus runs all three. **Example 3** ```text Input: trips = [[2, 10], [3, 4], [4, 6], [7, 8]] Output: 2 ``` The long trip needs its own bus, and the other three never overlap one another.

Overview: Given scheduled trips with start and end times, return the fewest buses needed so that no bus runs two trips at once, where a bus may start a trip the moment its previous one ends. It tests reasoning about maximum interval overlap and boundary handling.

A transit operator has a list of scheduled trips. Each trip has a start time and an end time, and a bus can run only one trip at a time. Return the smallest number of buses that can cover every trip. Implement `min_buses(trips)`, where `trips[i] = [start, end]`. ### Rules - Every trip satisfies `start < end`. The bus running trip `i` is busy from `start` up to, but not including, `end` (the half-open interval `[start, end)`). - A bus that finishes a trip at time `t` can start another trip at time `t`. - Any free bus can take any trip; travel between the end of one trip and the start of the next is not modeled. - The trips are not necessarily given in sorted order, and the same `[start, end]` pair may appear more than once; each occurrence is a separate trip that needs a bus. ### Output Return one integer: the minimum number of buses needed to run every trip. ### Constraints - `1 <= len(trips) <= 10^5` - `0 <= start < end <= 10^9` for every trip - All times and the answer fit in a signed 32-bit integer (no value exceeds 2^31 - 1). ### Examples **Example 1** ```text Input: trips = [[1, 4], [2, 5], [7, 9], [3, 6]] Output: 3 ``` At time 3, the trips `[1, 4]`, `[2, 5]` and `[3, 6]` are all running, so three buses are needed. Trip `[7, 9]` can reuse any of them. **Example 2** ```text Input: trips = [[1, 3], [3, 5], [5, 8]] Output: 1 ``` Each trip starts exactly when the previous one ends, so one bus runs all three.

Constraints

  • 1 <= len(trips) <= 10^5
  • 0 <= start < end <= 10^9 for every trip
  • Each trips[i] is a pair [start, end] of integers
  • All times and the answer fit in a signed 32-bit integer

Examples

Input: ([[1, 4], [2, 5], [7, 9], [3, 6]],)

Expected Output: 3

Explanation: Source Example 1: [1, 4], [2, 5] and [3, 6] all run at time 3; [7, 9] reuses a freed bus.

Input: ([[1, 3], [3, 5], [5, 8]],)

Expected Output: 1

Explanation: Source Example 2: back-to-back chain where each start equals the previous end, so one bus runs all three (half-open reuse).

Hints

  1. A bus is busy on [start, end), so a trip that ends at time t and a trip that starts at time t never conflict.
  2. Look at the single moment when the most trips are running at once: could fewer buses than that ever be enough?
  3. Processing events in time order makes it easier to know how many buses are busy when a new trip begins.

Loading coding console...

Show the approach

Approach

Algorithm: sort all start times and all end times separately. Sweep the start times in increasing order, keeping a pointer into the sorted end times. Before counting the trip that starts at time s, release every trip whose end is <= s; because a bus is busy only on [start, end), a bus freed at time s can take the new trip. The running count after adding the new trip is the number of trips in progress at time s, and the answer is the largest such count.

Invariant: when the trip at sorted position i is counted, active equals the number of already-counted trips (start <= s) whose end is still > s, plus the new trip. Any trip with end <= s has start < end <= s, so it was already counted, which keeps active non-negative and exact.

Correctness: (lower bound) the maximum count k is reached at some start time s where k trips are running simultaneously, and they need k different buses. (upper bound) Assign trips in order of start time to any free bus; when a trip starts, at most k - 1 other trips are running, so with k buses one is always free. The overlap can only increase at a trip's start, so checking start times alone finds the maximum.

Edge cases: touching endpoints (end == start) never conflict because ends are released with <=; identical trips each count separately; input order does not matter because both arrays are sorted; a single trip returns 1. All values stay below 2^31 - 1, so 32-bit integers suffice.

Time complexity:
O(n log n)
Space complexity:
O(n)