Skip to content
StackDecode
GreedyAnimated

Minimum Platforms

Sort arrivals and departures; track the most trains present at once.

Time

O(n log n)

Extra space

O(1)

Use it for

Resource counting for overlapping intervals.

How it works

  1. Split every train into two events: an arrival and a departure. Sort the arrivals and the departures separately.
  2. Sweep with two pointers. If the next arrival is at or before the next departure, a new platform is needed. Otherwise a train leaves and a platform is freed.
  3. Track the highest number of platforms in use. That maximum is the answer. O(n log n).

Watch it run

Loading animation…