Minimum Platforms
You are 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 wait
The first line contains space-separated integers that represent the arrival time of the trains.
The second line contains space-separated integers represent the corresponding end time of the trains.
The output should have an integer that represents the number of platforms needed to arrange the commute of all the trains.
Explanation: The arrival times of 5 trains are
0900 0940 0950 1100 1500. The departure times of 5 trains are 0910 1200 1120 1130 1900.The first platform will be used to run the trains that arrive at
0900 0940 1500. The second platform will be used to run the trains that arrive at 0950.
Sample Input 1
0900 1100 1235
1000 1200 1240
Sample Output 1
1
Sample Input 2
0900 0940 0950 1100 1500
0910 1200 1120 1130 1900
Sample Output 2
3
N = int(input('How many trains:'))
print("Please Enter the arrival times:")
arrival = [int(i) for i in input().split()][:N]
print("Please Enter the departure times:")
departure = [int(i) for i in input().split()][:N]
def minPlatform(arr, dep, n):
# Sort arrival and
# departure arrays
arr.sort()
dep.sort()
# number of platforms
plat_needed = 1
result = 1
i = 1
j = 0
while (i < n and j < n):
# platforms needed to be incremented or decremented
if (arr[i] <= dep[j]):
plat_needed += 1
i += 1
elif (arr[i] > dep[j]):
plat_needed -= 1
j += 1
# Update result if needed
if (plat_needed > result):
result = plat_needed
return result
print(minPlatform(arrival, departure, N))
Comments
Leave a comment