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

Platforms for the Sunday Timetable

heaps · intervals · sweep line · min-heap

A junction's Sunday timetable lists trains, where trains[i] = [arrival, departure] and arrival < departure. Each train needs a platform to itself from the moment it arrives until it leaves. A platform is free again at the departure time, so a train that arrives exactly as another departs can use that same platform.

Return the smallest number of platforms that lets every train pull straight in without waiting.

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…