Java Program to Find the Minimum Number of Platforms Required for a Railway Station

The railway station problem is the one of the most important problem usually asked in coding round interview to test the logic ability and problem resolving skill of the candidate.

The Railway Station Problem

In this problem, the arrival and departure time of trains are provided for a particular railway station. There are the two arrays named arrival[] and departure[] that denotes the arrival and departure time (24 hour clock) of the trains. We have to find the minimum number of platforms required for a station so that trains can run according to schedule.

Let's understand the problem through an example.

Example 1

Input: arrival[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00}

departure[] = {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). So, we require at least three platforms, otherwise the railway station will not be able to provide platforms for all the trains.

Let's consider another example.

Example 2

Input: arrival[] = {09:10, 01:00}

departure[] = {09:50, 01:30}

In the above given trimming, the first train is arriving at 10:20 AM and departed at 10:50 AM. Now, no train is allocated at that platform. Therefore, we require only one platform to allocate the train.

Solutions to the Problem

There are the following two possible solutions to find the minimum number of platforms required:

  • Naive Approach
  • Efficient Approach

Naive Approach

It is the simplest approach to find the minimum number of platforms required for a railway station. The solution can be achieved by using the two nested for loops.

The inner for loop for the counting the number of intervals that intersects with the interval denoted by the outer for loop. Once the inner loop execution ends, update the maximum value of the count. After that, proceed with the next iteration of the outer for loop. When the outer loop exits, we get the maximum value of the count. It is the number of platforms required for a railway station.

Time complexity for the above approach is O(n2) and the space complexity is O(1) because no extra space is used.

Algorithm

Let's implement the above approach in a Java program.

Platform.java

Output:

Minimum number of Platforms required: 3

We can optimize the above solution by using the greedy and efficient approach.

Efficient Approach

First, we will sort both the arrays of arrival and departure timing. The sorting logic makes easy to count the number of trains that have arrived at the platform but not yet departed.

The question is how to find the total number of platforms required at a time. We can find the total number of platforms by figure out the difference between arrival and departure time at that time. The maximum value of all times that we figure out above will be the final answer. There are the following two necessary conditions that we have checked in the following program:

  1. if(arrival[i]<=departure[j]) means, required one more platform. Increment the count as well as increment i.
  2. if(arrival[i]>departure[j]) means, we have an extra platform. We must free that platform for other train that are arriving. For the same, we decrement the count variable but increment the variable j. After each iteration of the while loop, update the answer with max(result, count).

In the above approach, we have used the sorting to sort the arrival and departure timing of the trains that take O(logn) time. Also, we have traverse over the array that takes O(n) time. Therefore, the overall time complexity is O(nlogn). The approach do not use any extra space, so the space complexity is O(1).

RailwayStation.java

Output:

Minimum Number of Platforms Required = 1

We observe that minimum number of platforms needed on the railway station is equal to the maximum number of trains that can be there at any given time on the railway station.

Conclusion

The naive approach is considerably complex and the program executes on a complexity of O(n2) whereas the efficient approach is comparatively more optimized and the time complexity is O(nlogn).