Minimum Platforms

medium

You are given the schedule of trains at a station as a list trains, where each train is [arrival, departure]. A platform can hold only one train at a time, and if one train arrives at the exact moment another departs they still need separate platforms.

Return the minimum number of platforms required so that no train ever has to wait.

Hints

How many platforms do you need at any single instant?
It's the maximum number of trains at the station at the same time — peak interval overlap.
Sort arrivals and departures separately and sweep them together, +1 on an arrival, -1 on a departure.

Common doubts

You only need the count of overlapping intervals over time, not which train is which. Two sorted event lists reproduce the timeline of arrivals and departures exactly.
A train arriving at the same instant another departs still overlaps it (they can't share a platform), so an equal-time arrival must be counted before the departure frees a platform.
The count only ever increases on an arrival, so any maximum is reached immediately after some arrival event — which is why the brute checks arrival times.

Interview follow-ups

Track the event times as you sweep; record the interval during which the running count equals the peak.
That becomes a scheduling/feasibility problem — with k platforms you'd greedily assign arrivals to freed platforms via a min-heap of departure times.

Fun facts

  • This is the interval-graph chromatic number in disguise: the minimum platforms equals the maximum clique of overlapping intervals, which for intervals equals the peak overlap.
  • The same sweep counts peak concurrent connections on a server or peak simultaneous calls in a phone network.

Asked at

AmazonGoogleMicrosoftGoldman Sachs
Frequently Sometimes Occasionally
Example 1
Input: trains = [[900,910],[940,1200],[950,1120],[1100,1130],[1500,1900],[1800,2000]]
Output: 3
Between 950 and 1120, trains [940,1200], [950,1120], and [1100,1130] overlap — three platforms are needed.
Example 2
Input: trains = [[1,2],[3,4]]
Output: 1
The two trains never overlap, so a single platform suffices.
Constraints

- 1 <= trains.length <= 10^5 - trains[i] == [arrival_i, departure_i] with arrival_i <= departure_i

Solve this problem →