Minimum Platforms | Practice | GeeksforGeeks

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 is kept waiting.
Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms.

Example 1:
Input: n = 6 
arr[] = {0900, 0940, 0950, 1100, 1500, 1800}
dep[] = {0910, 1200, 1120, 1130, 1900, 2000}
Output: 3
Explanation: 
Minimum 3 platforms are required to 
safely arrive and depart all trains.
Example 2:
Input: n = 3
arr[] = {0900, 1100, 1235}
dep[] = {1000, 1200, 1240}
Output: 1
Explanation: Only 1 platform is required to 
safely manage the arrival and departure 
of all trains. 


Your Task:
You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits.
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 may be > 59).

Expected Time Complexity: O(nLogn)
Expected Auxiliary Space: O(n)

Constraints:
1 ≤ n ≤ 50000
0000 ≤ A[i] ≤ D[i] ≤ 2359
+3
abhiraj643110 hours ago
C++ | priority queue | easy explained || 
Approach : let's think little practically here what we need is priority wise arranging  trains, such that we use least platforms.
say we have an intelligent  data structure whose size represents the total platforms and maintains a pointer to a train of  lowest departure time among present trains on all platforms.   
when new train comes (consider we have only one platform initially with only one train's departure data maintanded)
based on new train's arrival time we can 
compare the lowest daparture time of existing trains with the arrival time of this new train.if there exists such train on platforms whose departure is less than arrival time of this new train , then we will just add this new train to that platform and now data structure will maintain info of new train''s departure time and will remove the old train's data from that particular platformif arrival time is greater than the existing train's lowest departure time time we will just add new platform for the train
compare the lowest daparture time of existing trains with the arrival time of this new train.
if there exists such train on platforms whose departure is less than arrival time of this new train , then we will just add this new train to that platform and now data structure will maintain info of new train''s departure time and will remove the old train's data from that particular platform
if arrival time is greater than the existing train's lowest departure time time we will just add new platform for the train
 ( eventually platforms will dynamically change in the end the the required platforms  will be equal to the size of data structure) 
 
we need priority queue which shows minimum element in the queue.
 
	priority_queue<int, vector<int> , greater<int>> pq;
Solution :
store pair of arrival and departure into vector pairsort vector pair as per the arrival time of trainsget min heapif lowest departure time in existing platform queue is less     than arrival time of new train then pop that train and add new train's departure timeelse add new trains departure time to queue : assigning new platform to new train.return size of platform queue. 
store pair of arrival and departure into vector pair
sort vector pair as per the arrival time of trains
get min heap
if lowest departure time in existing platform queue is less     than arrival time of new train then pop that train and add new train's departure time
else add new trains departure time to queue : assigning new platform to new train.
return size of platform queue. 
class Solution{
    public:
    //Function to find the minimum number of platforms required at the
    //railway station such that no train waits.
    int findPlatform(int arr[], int dep[], int n)
    {
    	
    	vector<pair<int,int>> vp;
    	for(int i=0;i<n;i++){
    	    vp.push_back({arr[i],dep[i]});
    	}
    	
    	sort(vp.begin(),vp.end());

    	priority_queue<int, vector<int> , greater<int>> pq;
    	
    	
    	for(int i=0;i<n;i++){
    	    pq.push(vp[i].second);
    	    if(pq.top()<vp[i].first) {pq.pop();}
    	}
    	return pq.size();
    
    }
};
 
0
bibhore0215 hours ago
using pq...all test case passed
int findPlatform(int arr[], int dep[], int n)   {             vector<pair<int,int>> itvls;       for(int i=0;i<n;i++)       itvls.push_back({arr[i],dep[i]});              sort(itvls.begin(),itvls.end());              priority_queue <int, vector<int>, greater<int> > pq;              pq.push(itvls[0].second);              for(int i=1;i<n;i++)       {           if(pq.top()>=itvls[i].first)           {              pq.push(itvls[i].second);            }           else           {               pq.pop();               pq.push(itvls[i].second);           }                  }              return pq.size();                }};
0
abhinayabhi1412 days ago
 sort(arr,arr+n);
    sort(dep,dep+n);
    int count=1,ans=1;
    int j=0,i=1;
    while(i<n && j<n){
        if(arr[i]<=dep[j]){
            count++;
            i++;
        }
        else{
            count--;
            j++;
        }
        if(count>ans)ans=count;
    }
    return ans;
0
yogya25singhal3 days ago
 sort(arr,arr+n);
    sort(dep,dep+n);
    int count=1,ans=1;
    int j=0,i=1;
    while(i<n && j<n){
        if(arr[i]<=dep[j]){
            count++;
            i++;
        }
        else{
            count--;
            j++;
        }
        if(count>ans)ans=count;
    }
    return ans;
    
+1
jaydhurat3 days ago
class Solution
{
   //Function to find the minimum number of platforms required at the
   //railway station such that no train waits.
   static int findPlatform(int a[], int dep[], int n)
   {
       // add your code here
       Arrays.sort(a);
       Arrays.sort(dep);
       int pf = 1;
       int max =1;
       
       int i = 1;
       int j=0;
       
       while(i<n && j<n)
       {
           if(a[i]<=dep[j])
           {
               pf++;
               
               i++;
           }
           else if( a[i]>dep[j])
           {
               pf--;
               j++;
              
           }
           if(pf>max)
           {
               max=pf;
           }
       }
       return max;
   }
}
0
sanjeevranjan12094 days ago
Python full solution:

class Solution:    
    #Function to find the minimum number of platforms required at the
    #railway station such that no train waits.
    def minimumPlatform(self,n,arr,dep):
        dep.sort()
        arr.sort()
        i,j=1,0
        result,platform = 1,1
        while (i<n and j<n):
            if arr[i]<=dep[j]:
                i+=1
                platform+=1
            elif arr[i]>dep[j]:
                j+=1
                platform-=1
            if result<platform:
                result = platform
                
        return result
0
sanskritisrivastava201914 days ago
Java Solution:
static int findPlatform(int arr[], int dep[], int n)
   {
       Arrays.sort(arr);//sorting arrival and departure time
       Arrays.sort(dep);//to know till when platforms will be busy
       int platform_needed=1,res=1;
       int i=1,j=0;
       while(i<n && j<n){
           if(arr[i]<=dep[j]){ //platform is busy w former arrival
               platform_needed++;
               i++;
           }else if(arr[i]>dep[j]){
               platform_needed--; //current platform_needed will reduce
               j++;               //checking for next departure
           }
           res = Math.max(platform_needed,res); //max platform needed overall
       }
       return res;
   }
0
hum_toh_ram_bharose4 days ago
c++ short and crisp
   int findPlatform(int arr[], int dep[], int n)   {   int need=1; int j=0; int max=1;   sort(arr,arr+n);   sort(dep,dep+n);   for(int i=1;i<n;i++){       if(arr[i]>dep[j]){        j++; }       else       need++;       if(need>max)       max=need;   }   return max;   }
-1
ashivamsahu144 days ago
CPP Solution 
sort(arr,arr+n);
    	sort(dep,dep+n);
    	int mx=1;
    	int platform=1;
    	int start=1;
    	int end=0;
    	while(start<n && end<n){
    	    
    	    if(arr[start] > dep[end]){
    	        end++;
    	        platform--;
    	    }
    	    else if(arr[start] <= dep[end]){
    	        platform++;
    	        start++;
    	    }
    	    mx=max(platform,mx);
    	}
    	
    	return mx;
0
iamsamrat165 days ago
Simple Implementation of LineSweeping Algorithm:
static int findPlatform(int arr[], int dep[], int n)
    {
        int lmax = 0,gmax=0;
        Arrays.sort(arr);
        Arrays.sort(dep);
        int i =0,j=0;
        while(i<arr.length && j<dep.length) {
            if(arr[i]<=dep[j])
            {
                lmax++;
                i++;
            }
            else{
                j++;
                lmax--;
            }
            gmax = Math.max(lmax, gmax);
        }
        return gmax;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.