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
- Split every train into two events: an arrival and a departure. Sort the arrivals and the departures separately.
- 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.
- Track the highest number of platforms in use. That maximum is the answer. O(n log n).
Watch it run
Loading animation…