Answer to Question #207389 in Python for luifer

Question #207389

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

Input

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.

Output

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


1
Expert's answer
2021-06-17T04:39:47-0400




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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS