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 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 events 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.
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 the 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.
Note : This approach assumes that trains are arriving and departing on same date.
C++
// Program to find minimum number of platforms // required on a railway station #include<iostream> #include<algorithm> using namespace std; // Returns minimum number of platforms reqquired int findPlatform(int arr[], int dep[], int n) { // Sort arrival and departure arrays sort(arr, arr+n); sort(dep, dep+n); // plat_needed indicates number of platforms // needed at a time int plat_needed = 1, result = 1; int i = 1, j = 0; // Similar to merge in merge sort to process // all events in sorted order while (i < n && j < n) { // If next event in sorted order is arrival, // increment count of platforms needed if (arr[i] <= dep[j]) { plat_needed++; i++; // Update result if needed if (plat_needed > result) result = plat_needed; } // Else decrement count of platforms needed else { plat_needed--; j++; } } return result; } // Driver program to test methods of graph class 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; } |
Java
// Program to find minimum number of platforms import java.util.*; class GFG { // Returns minimum number of platforms reqquired static int findPlatform(int arr[], int dep[], int n) { // Sort arrival and departure arrays Arrays.sort(arr); Arrays.sort(dep); // plat_needed indicates number of platforms // needed at a time int plat_needed = 1, result = 1; int i = 1, j = 0; // Similar to merge in merge sort to process // all events in sorted order while (i < n && j < n) { // If next event in sorted order is arrival, // increment count of platforms needed if (arr[i] <= dep[j]) { plat_needed++; i++; // Update result if needed if (plat_needed > result) result = plat_needed; } // Else decrement count of platforms needed else { plat_needed--; j++; } } return result; } // Driver program to test methods of graph class public static void main(String[] args) { int arr[] = {900, 940, 950, 1100, 1500, 1800}; int dep[] = {910, 1200, 1120, 1130, 1900, 2000}; int n = arr.length; System.out.println("Minimum Number of Platforms Required = " + findPlatform(arr, dep, n)); } } |
Python3
# Program to find minimum # number of platforms # required on a railway # station # Returns minimum number # of platforms reqquired def findPlatform(arr,dep,n): # Sort arrival and # departure arrays arr.sort() dep.sort() # plat_needed indicates # number of platforms # needed at a time plat_needed = 1 result = 1 i = 1 j = 0 # Similar to merge in # merge sort to process # all events in sorted order while (i < n and j < n): # If next event in sorted # order is arrival, # increment count of # platforms needed if (arr[i] < dep[j]): plat_needed+=1 i+=1 # Update result if needed if (plat_needed > result): result = plat_needed # Else decrement count # of platforms needed else: plat_needed-=1 j+=1 return result # driver code arr = [900, 940, 950, 1100, 1500, 1800] dep = [910, 1200, 1120, 1130, 1900, 2000] n = len(arr) print("Minimum Number of Platforms Required = ", findPlatform(arr, dep, n)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find minimum number // of platforms using System; class GFG { // Returns minimum number of platforms // reqquired static int findPlatform(int []arr, int []dep, int n) { // Sort arrival and departure arrays Array.Sort(arr); Array.Sort(dep); // plat_needed indicates number of // platforms needed at a time int plat_needed = 1, result = 1; int i = 1, j = 0; // Similar to merge in merge sort // to process all events in sorted // order while (i < n && j < n) { // If next event in sorted order // is arrival, increment count // of platforms needed if (arr[i] <= dep[j]) { plat_needed++; i++; // Update result if needed if (plat_needed > result) result = plat_needed; } // Else decrement count of // platforms needed else { plat_needed--; j++; } } return result; } // Driver program to test methods of // graph class public static void Main() { int []arr = {900, 940, 950, 1100, 1500, 1800}; int []dep = {910, 1200, 1120, 1130, 1900, 2000}; int n = arr.Length; Console.Write("Minimum Number of " + " Platforms Required = " + findPlatform(arr, dep, n)); } } // This code os contributed by nitin mittal. |
PHP
<?php // PHP Program to find minimum number // of platforms required on a railway // station // Returns minimum number of // platforms reqquired function findPlatform($arr, $dep, $n) { // Sort arrival and // departure arrays sort($arr); sort($dep); // plat_needed indicates // number of platforms // needed at a time $plat_needed = 1; $result = 1; $i = 1; $j = 0; // Similar to merge in // merge sort to process // all events in sorted order while ($i < $n and $j < $n) { // If next event in sorted // order is arrival, increment // count of platforms needed if ($arr[$i] <= $dep[$j]) { $plat_needed++; $i++; // Update result if needed if ($plat_needed > $result) $result = $plat_needed; } // Else decrement count // of platforms needed else { $plat_needed--; $j++; } } return $result; } // Driver Code $arr = array(900, 940, 950, 1100, 1500, 1800); $dep = array(910, 1200, 1120, 1130, 1900, 2000); $n = count($arr); echo "Minimum Number of Platforms Required = " , findPlatform($arr, $dep, $n); // This code os contributed by anuj_67. ?> |
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 for a Railway/Bus Station | Set 2 (Map based approach)
This article is contributed by Shivam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Minimum number of operations required to reduce N to 1
- Minimum number of bottles required to fill K glasses
- Minimum number of days required to complete the work
- Minimum number of operations required to delete all elements of the array
- Minimum boxes required to carry all gifts
- Minimum swaps required to make a binary string alternating
- Minimum array insertions required to make consecutive difference <= K
- Find maximum distance between any city and station
- Paper Cut into Minimum Number of Squares
- Find minimum number of coins that make a given value
- Minimum number of 1's to be replaced in a binary array
- Greedy Algorithm to find Minimum number of Coins
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Find minimum number of currency notes and values that sum to given amount
- Minimum number of adjacent swaps for arranging similar elements together



