VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-platforms-required-railwaybus-station/

⇱ Minimum Platforms Required - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Platforms Required

Last Updated : 3 Apr, 2026

Given two arrays arr[] and dep[], that represent the arrival and departure time of i-th train respectively. Find the minimum number of platforms required so that no train has to wait. If the departure time of one train is the same as the arrival time of another train, both trains cannot use the same platform at that time.

Note: Time intervals are in the 24-hour format (HHMM), where the first two characters represent hour (between 00 to 23) and the last two characters represent minutes (this will be <= 59 and >= 0). Leading zeros for hours less than 10 are optional (e.g., 0900 is the same as 900).

Examples:

Input: arr[] = [1000, 935, 1100], dep[] = [1200, 1240, 1130]
Output: 3
Explanation: We need 3 platforms for the arrival of these trains because all three trains have overlapping time.

👁 409843035

Input: arr[] = [900, 1235, 1100], dep[] = [1000, 1240, 1200]
Output: 1
Explanation: Only 1 platform is required for all the three trains because the departure time of each train is less then arrival time of next train.

[Naive Approach] Using Two Nested Loops - O(n2) time and O(1) space

  • Run two nested loops, the outer loop from start to end and the inner loop from i+1 to end.
  • For every iteration of the outer loop, find the count of intervals that intersect with the current interval.
  • Update the answer with the maximum count of overlap in each iteration of the outer loop.

Output
3

[Expected Approach] Traverse all events in sorted order - O(n log(n)) Time and O(1) Space

The idea is to consider all events in sorted order. Once the events are in sorted order, trace the number of trains at any time keeping track of trains that have arrived, but not departed.

Below are the steps

  • Sort arr[] in ascending order
  • Sort dep[] in ascending order
  • Now traverse all events in ascending order, we mainly use the idea of the merge() function of merge sort here to merge two sorted arrays.

arr[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00}
dep[] = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}

All events are sorted by time.
Total platforms at any time can be obtained by subtracting total departures from total arrivals by that time.

Time Event Type Count at this Time
9:00 Arrival 1
9:10 Departure 0
9:40 Arrival 1
9:50 Arrival 2
11:00 Arrival 3
11:20 Departure 2
11:30 Departure 1
12:00 Departure 0
15:00 Arrival 1
18:00 Arrival 2
19:00 Departure 1
20:00 Departure 0

Minimum Platforms = Maximum platforms needed at any time = 3


Output
3

[Expected Approach 2] Sweep Line

The Sweep Line Algorithm is an efficient technique for solving interval-based problems.

It works by treating each train's arrival and departure times as events on a timeline. By processing these events in time order, we can track how many trains are at the station at any moment, which directly gives the number of platforms needed at that time. The maximum overlap of trains during this process determines the minimum number of platforms required.

Step by Step implementation:

  • Create an array v[] of size greater than the maximum departure time. This array will help track the number of platforms needed at each time.
  • Mark arrivals and departures:
  • For each arrival time, increment v[arrival_time] by 1, indicating that a platform is needed.
  • For each departure time, decrement v[departure_time + 1] by 1, indicating that a platform is freed as the train has left.
  • Iterate through v[] and compute the cumulative sum.
  • The running sum keeps track of the number of trains present at any given time.
  • The maximum value encountered represents the minimum number of platforms required.

Output
3

Time Complexity: O(n + k), where n is the number of trains and k is the maximum value present in the arrays.
Auxiliary space: O(k), where k is the maximum value present in both the arrays.

Comment