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)

We have already discussed its simple and sorting based solutions in below post.

Minimum Number of Platforms Required for a Railway/Bus Station.

In this post, we are inserting all the arrival and departure times in a multimap. The first value of element in multimap tells the arrival/departure time and second value tells whether it’s arrival or departure represented by ‘a’ or ‘d’ respectively.

If its arrival then do increment by 1 otherwise decrease value by 1. If we are taking the input from STDIN then we can directly insert the times in the multimap and no need to store the times in the array.

`// Program to find minimum number of platforms ` `// required on a railway station ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findPlatform(` `int` `arr[], ` `int` `dep[], ` `int` `n) ` `{ ` ` ` `// Insert all the times (arr. and dep.) ` ` ` `// in the multimap. ` ` ` `multimap<` `int` `, ` `char` `> order; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If its arrival then second value ` ` ` `// of pair is 'a' else 'd' ` ` ` `order.insert(make_pair(arr[i], ` `'a'` `)); ` ` ` `order.insert(make_pair(dep[i], ` `'d'` `)); ` ` ` `} ` ` ` ` ` `int` `result = 0; ` ` ` `int` `plat_needed = 0; ` ` ` ` ` `multimap<` `int` `, ` `char` `>::iterator it = order.begin(); ` ` ` ` ` `// Start iterating the multimap. ` ` ` `for` `(; it != order.end(); it++) { ` ` ` ` ` `// If its 'a' then add 1 to plat_needed ` ` ` `// else minus 1 from plat_needed. ` ` ` `if` `((*it).second == ` `'a'` `) ` ` ` `plat_needed++; ` ` ` `else` ` ` `plat_needed--; ` ` ` ` ` `if` `(plat_needed > result) ` ` ` `result = plat_needed; ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 900, 940, 950, 1100, 1500, 1800 }; ` ` ` `int` `dep[] = { 910, 1200, 1120, 1130, 1900, 2000 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Minimum Number of Platforms Required = "` ` ` `<< findPlatform(arr, dep, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3

**Method 2:** If all the arrival and departure occur on the same day then we can use and auxiliary array to compute the required number of the platform in O(n).

Whenever an arrival occurs we increase the count of the required platform when a departure occurs we decrease the required platform at that point of time, after that, we take the cumulative sum, which would provide the required number of platform for all point of time, out of these values maximum value is our answer.

`// Program to find minimum number of platforms ` `// required on a railway station ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `minPlatform(` `int` `arrival[], ` `int` `departure[], ` `int` `n) ` `{ ` ` ` ` ` `// as time range from 0 to 2359 in 24 hour clock, ` ` ` `// we declare an array for values from 0 to 2360 ` ` ` `int` `platform[2361] = {}; ` ` ` `int` `requiredPlatform = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// increment the platforms for arrival ` ` ` `++platform[arrival[i]]; ` ` ` ` ` `// once train departs we decrease the platform count ` ` ` `--platform[departure[i] + 1]; ` ` ` `} ` ` ` ` ` `// We are running loop till 2361 because maximum time value ` ` ` `// in a day can be 23:60 ` ` ` `for` `(` `int` `i = 1; i < 2361; i++) { ` ` ` ` ` `// taking cumulative sum of platform give us required ` ` ` `// number of platform fro every minute ` ` ` `platform[i] = platform[i] + platform[i - 1]; ` ` ` `requiredPlatform = max(requiredPlatform, platform[i]); ` ` ` `} ` ` ` `return` `requiredPlatform; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 900, 940, 950, 1100, 1500, 1800 }; ` ` ` `int` `dep[] = { 910, 1200, 1120, 1130, 1900, 2000 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Minimum Number of Platforms Required = "` ` ` `<< minPlatform(arr, dep, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3

This article is contributed by **Jatin Goyal** and **Abhinav Kumar Singh**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum increment or decrement required to sort the array | Top-down Approach
- Minimum number of distinct powers of 2 required to express a given binary number
- Minimum number of swaps required to sort an array of first N number
- Minimum total cost incurred to reach the last station
- Minimum number of changes required to make the given array an AP
- Minimum number of swaps required to sort an array
- Minimum number of swaps required to sort an array | Set 2
- Minimum deletions required such that any number X will occur exactly X times
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of towers required such that every house is in the range of at least one tower
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum number of segments required such that each segment has distinct elements
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum number of colors required to color a Circular Array
- Minimum number of Circular obstacles required to obstruct the path in a Grid
- Find the minimum number of operations required to make all array elements equal
- Sort the strings based on the numbers of matchsticks required to represent them
- Sort numbers based on count of letters required to represent them in words
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of given operations required to convert a permutation into an identity permutation