Problem 453780 · medium · Level 04 Non-Linear Data Structures

Minimum Railway Platforms

heaps · intervals · sweep line · min-heap

A station receives trains, where trains[i] = [arrival, departure] with arrival < departure. A train occupies a platform from its arrival up to its departure. A platform becomes free at the departure time, and a train arriving exactly when another departs may reuse that platform.

Return the minimum number of platforms needed so that no train ever waits.

Examples

Input:  trains = [[9, 11], [10, 12], [11, 13], [12, 14]]
Output: 2
Explanation: at t=10 two trains are present. The train arriving at 11 takes the platform
             freed at 11, and the one arriving at 12 takes the platform freed at 12.

Input:  trains = [[1, 4], [2, 3], [3, 6]]
Output: 2
Explanation: at t=2 two trains overlap; the 3->6 train reuses the platform freed at 3.

Constraints

  • 0 <= len(trains) <= 10**5, 0 <= arrival < departure <= 10**9
  • Target complexity: O(n log n).

Goals

  • Sort intervals by start and sweep through them once
  • Track active intervals by their end times in a min-heap
  • Report the peak number of simultaneously active intervals
Starting Python…