Given arrival and departure times of all trains that reach a railway station, find the minimum number of platforms required for the railway station so that no train waits.
We are given two arrays which represent arrival and departure times of trains that stop

Examples:

Input:  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}
Output: 3
There are at-most three trains at a time (time between 11:00 to 11:20)

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We need to find the maximum number of trains that are there on the given railway station at a time. A Simple Solution is to take every interval one by one and find the number of intervals that overlap with it. Keep track of maximum number of intervals that overlap with an interval. Finally return the maximum value. Time Complexity of this solution is O(n2).

We can solve the above problem in O(nLogn) time. The idea is to consider all evens in sorted order. Once we have all events in sorted order, we can trace the number of trains at any time keeping track of trains that have arrived, but not departed.

[ad type=”banner”]

For example consider the above example.

    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 sorted by time.
Total platforms at any time can be obtained by subtracting total 
departures from total arrivals by that time.
 Time     Event Type     Total Platforms Needed 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 needed on railway station = Maximum platforms 
                                              needed at any time 
                                           = 3  

Following is C++ implementation of above approach. Note that the implementation doesn’t create a single sorted list of all events, rather it individually sorts arr[] and dep[] arrays, and then uses merge process of merge sort to process them together as a single sorted array.

[pastacode lang=”c” manual=”%2F%2F%20Program%20to%20find%20minimum%20number%20of%20platforms%20required%20on%20a%20railway%20station%0A%23include%3Ciostream%3E%0A%23include%3Calgorithm%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Returns%20minimum%20number%20of%20platforms%20reqquired%0Aint%20findPlatform(int%20arr%5B%5D%2C%20int%20dep%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%2F%2F%20Sort%20arrival%20and%20departure%20arrays%0A%20%20%20sort(arr%2C%20arr%2Bn)%3B%0A%20%20%20sort(dep%2C%20dep%2Bn)%3B%0A%20%0A%20%20%20%2F%2F%20plat_needed%20indicates%20number%20of%20platforms%20needed%20at%20a%20time%0A%20%20%20int%20plat_needed%20%3D%201%2C%20result%20%3D%201%3B%0A%20%20%20int%20i%20%3D%201%2C%20j%20%3D%200%3B%0A%20%0A%20%20%20%2F%2F%20Similar%20to%20merge%20in%20merge%20sort%20to%20process%20all%20events%20in%20sorted%20order%0A%20%20%20while%20(i%20%3C%20n%20%26%26%20j%20%3C%20n)%0A%20%20%20%7B%0A%20%20%20%20%20%20%2F%2F%20If%20next%20event%20in%20sorted%20order%20is%20arrival%2C%20increment%20count%20of%0A%20%20%20%20%20%20%2F%2F%20platforms%20needed%0A%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3C%20dep%5Bj%5D)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20plat_needed%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20if%20(plat_needed%20%3E%20result)%20%20%2F%2F%20Update%20result%20if%20needed%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20plat_needed%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20else%20%2F%2F%20Else%20decrement%20count%20of%20platforms%20needed%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20plat_needed–%3B%0A%20%20%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%7D%0A%20%0A%20%20%20return%20result%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20methods%20of%20graph%20class%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B900%2C%20940%2C%20950%2C%201100%2C%201500%2C%201800%7D%3B%0A%20%20%20%20int%20dep%5B%5D%20%3D%20%7B910%2C%201200%2C%201120%2C%201130%2C%201900%2C%202000%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20cout%20%3C%3C%20%22Minimum%20Number%20of%20Platforms%20Required%20%3D%20%22%0A%20%20%20%20%20%20%20%20%20%3C%3C%20findPlatform(arr%2C%20dep%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”c” highlight=”” provider=”manual”/]

Output:

Minimum Number of Platforms Required = 3

Algorithmic Paradigm: Dynamic Programming

Time Complexity: O(nLogn), assuming that a O(nLogn) sorting algorithm for sorting arr[] and dep[].

Minimum Number of Platforms Required = 3

Algorithmic Paradigm: Dynamic Programming

Time Complexity: O(nLogn), assuming that a O(nLogn) sorting algorithm for sorting arr[] and dep[].

[ad type=”banner”]