forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum_Platforms.cpp
29 lines (28 loc) · 968 Bytes
/
Minimum_Platforms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Given arrival and departure times of all trains that reach a railway station. Your task is to find the minimum number of platforms required for the railway station so that no train waits.
Note: Consider that all the trains arrive on the same day and leave on the same day. Also, arrival and departure times will not be same for a train, but we can have arrival time of one train equal to departure of the other.
In such cases, we need different platforms, i.e at any given instance of time, same platform can not be used for both departure of a train and arrival of another.
*/
//solution
int findPlatform(int arr[], int dep[], int n)
{
sort(arr,arr+n);
sort(dep,dep+n);
int platform=1;
int i=1,j=0,ans=1;
while(i<n && j<n)
{
if(dep[j]>=arr[i]) //platform needed due to time clashing
{
platform++;
i++;
if(platform>ans) ans=platform;
}
else
{
j++;
platform--;
}
}
return ans;
}