GeeksforGeeks Medium

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.
Constraints
  • 1 <= arr.size() = dep.size() <= 10⁵
  • 0000 <= arr[i] <= dep[i] <= 2359
  • Times are 24-hour HHMM values on the same day.
greedysortingtwo-pointers
Open on GeeksforGeeks ↗
02

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 to spot this pattern

"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.

03

Approach

Try it first

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).

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def minPlatform(self, arr: list[int], dep: list[int]) -> int:
▶3 arr = sorted(arr)
▶4 dep = sorted(dep)
▶5 n = len(arr)
▶6 i = j = 0
▶7 cur = best = 0
▶8 while i < n:
▶9 if arr[i] <= dep[j]:
▶10 cur += 1
▶11 best = max(best, cur)
▶12 i += 1
▶13 else:
▶14 cur -= 1
▶15 j += 1
▶16 return best
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station0most0arrivals and departures sorted on their own
at station0
most0
The two lists are sorted separately, so the first arrival no longer lines up with its own departure. That is fine: the count of trains in the station is just arrivals so far minus departures so far.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:009:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station1most1train in at 9:00 → 1 at the station, new most
at station1
most1
The next arrival, 9:00, comes before the next departure at 9:10, so nobody has freed a platform yet. 1 trains are now in the station, the most so far.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:009:109:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station0most1a train leaves at 9:10 → 0 at the station
at station0
The departure at 9:10 is earlier than the next arrival at 9:40, so a platform frees up before that train comes in. Which train leaves does not matter, only that one platform is free again.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:009:409:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station1most1train in at 9:40 → 1 at the station
at station1
most1
The next arrival, 9:40, comes before the next departure at 11:20, so nobody has freed a platform yet. 1 trains are now in the station, not more than the 1 seen before.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:009:509:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station2most2train in at 9:50 → 2 at the station, new most
at station2
most2
The next arrival, 9:50, comes before the next departure at 11:20, so nobody has freed a platform yet. 2 trains are now in the station, the most so far.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:0011:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station3most3train in at 11:00 → 3 at the station, new most
at station3
most3
The next arrival, 11:00, comes before the next departure at 11:20, so nobody has freed a platform yet. 3 trains are now in the station, the most so far.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:0011:209:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station2most3a train leaves at 11:20 → 2 at the station
at station2
The departure at 11:20 is earlier than the next arrival at 15:00, so a platform frees up before that train comes in. Which train leaves does not matter, only that one platform is free again.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:0011:309:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station1most3a train leaves at 11:30 → 1 at the station
at station1
The departure at 11:30 is earlier than the next arrival at 15:00, so a platform frees up before that train comes in. Which train leaves does not matter, only that one platform is free again.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:0012:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station0most3a train leaves at 12:00 → 0 at the station
at station0
The departure at 12:00 is earlier than the next arrival at 15:00, so a platform frees up before that train comes in. Which train leaves does not matter, only that one platform is free again.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:0015:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station1most3train in at 15:00 → 1 at the station
at station1
most3
The next arrival, 15:00, comes before the next departure at 19:00, so nobody has freed a platform yet. 1 trains are now in the station, not more than the 3 seen before.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:0018:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station2most3train in at 18:00 → 2 at the station
at station2
most3
The next arrival, 18:00, comes before the next departure at 19:00, so nobody has freed a platform yet. 2 trains are now in the station, not more than the 3 seen before.
9:00 to 9:109:40 to 12:009:50 to 11:2011:00 to 11:3015:00 to 19:0018:00 to 20:009:0011:0013:0015:0017:0019:00arrivalsdeparts9:009:409:5011:0015:0018:009:1011:2011:3012:0019:0020:00at station2most3answer: 3 platforms
return3
All arrivals are handled; the departures left over can only lower the count. The busiest moment had 3 trains in the station together, so 3 platforms are enough and fewer would make a train wait.
05

Common pitfalls

Letting a departure go first on a tie

✗ Wrong
if arr[i] < dep[j]:
✓ Right
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

✗ Wrong
trains = sorted(zip(arr, dep))
dep = [d for _, d in trains]
✓ Right
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.

06

Edge cases

A train that arrives and departs in the same minute

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.

Times with no leading zero, like 900

HHMM numbers still sort in time order because a larger hour always gives a larger number, so 900 sorts before 1000 without any conversion.

07

Complexity

Time
O(n log n)
Space
O(n)
Sorting the two lists costs O(n log n) and the sweep moves each pointer at most n times. Python's sorted makes copies; sorting in place, as the C++ and Java code does, needs only O(1) extra space apart from the sort itself.
08

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.