Minimum Platforms
Minimum Platforms is a GFG problem (Medium). arr[i] and dep[i] are the arrival and departure times of train i, all on the same day, written as 24-hour HHMM numbers. Return the fewest platforms the station needs so that no train waits.
- A platform holds one train at a time. If a train arrives at the same minute another departs, both are at the station in that minute and need two platforms.
- With up to 10⁵ trains, comparing every pair of trains (O(n²)) is too slow.
- 1 <= arr.size() = dep.size() <= 10⁵
- 0000 <= arr[i] <= dep[i] <= 2359
- Times are 24-hour HHMM values on the same day.
Intuition
The minimum number of platforms is the largest number of trains at the station at the same moment. That count only changes when a train arrives (+1) or leaves (−1), so it is enough to walk through those events in time order.
Which train leaves does not matter, only when. So sort the arrival times and the departure times as two separate lists, then merge them like two sorted lists: always take the earlier event next. On a tie, take the arrival first, because the departing train is still standing at its platform during that minute. The highest count seen along the way is the answer.
"How many things overlap at the busiest moment" for a list of start and end times is a sweep over sorted events. Meeting Rooms II is the same problem with meetings instead of trains.
Approach
Before reading on, decide what the platform count is in terms of trains present at one moment. Then check whether you need to know which arrival belongs to which departure, and what should happen when an arrival and a departure share a minute. Aim for O(n log n).
Sort the two lists separately
Sort arr and dep on their own. Pairing is lost, but the number of trains present at time t is just arrivals so far minus departures so far, and that does not depend on which train is which.
Walk both lists with two pointers
Keep i on the next arrival and j on the next departure, with cur trains present and best the most so far. The earlier of arr[i] and dep[j] is the next thing that happens at the station.
An arrival at or before the next departure needs a platform
If arr[i] <= dep[j], a train comes in before anyone leaves, so add 1 to cur, update best, and move i. The <= handles the shared minute: the departing train has not left yet when the new one pulls in.
Otherwise a train leaves
If dep[j] < arr[i], the next event is a departure, so a platform frees up: subtract 1 from cur and move j. Stop once every arrival is handled; departures after the last arrival can only lower the count.
Code in Python, C++ and Java
Common pitfalls
Letting a departure go first on a tie
if arr[i] < dep[j]:
if arr[i] <= dep[j]:
With <, a train arriving at 10:00 is placed on the platform of a train that departs at 10:00, but both are there in that minute. For arr = [900, 1000], dep = [1000, 1100] the answer is 2, and the strict test returns 1.
Sorting the trains as pairs instead of two lists
trains = sorted(zip(arr, dep)) dep = [d for _, d in trains]
arr = sorted(arr) dep = sorted(dep)
Ordered by arrival, the departures are no longer in time order, so the pointer can wait on a late departure while an earlier one has already happened. For arr = [1000, 1030, 1100], dep = [1120, 1040, 1220] the 10:30 train leaves at 10:40, before the 11:00 arrival, so 2 platforms are enough. The paired version still waits on 11:20 and returns 3.
Edge cases
arr[i] == dep[i] is allowed. The arrival is counted first because of <=, so the train holds a platform for that minute and leaves on a later step.
HHMM numbers still sort in time order because a larger hour always gives a larger number, so 900 sorts before 1000 without any conversion.
Complexity
sorted makes copies; sorting in place, as the C++ and Java code does, needs only O(1) extra space apart from the sort itself.Minimum Platforms FAQ
Can this be done in O(n) time?
Yes, because times are limited to one day. Make an array of 2361 counters, add 1 at each arrival time and subtract 1 at the minute after each departure, then take running totals; the largest total is the answer. It skips sorting but uses an array sized by the clock, not by the number of trains.
What changes if trains can run past midnight?
Turn each time into minutes and add 24 × 60 to any departure that is earlier than its arrival. The same sort-and-sweep then works on the longer timeline.
How do I find which platform each train uses?
Then the pairing matters. Sort trains by arrival and keep a min-heap of (departure, platform) for occupied platforms. A train takes the platform on top if that train left before it arrives, and opens a new platform otherwise.