Java Program to Find the Minimum Number of Platforms Required for a Railway StationThe 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 ProblemIn 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 1Input: 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 atmost 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 2Input: 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 ProblemThere are the following two possible solutions to find the minimum number of platforms required:
Naive ApproachIt 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(n^{2}) 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 ApproachFirst, 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:
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. ConclusionThe naive approach is considerably complex and the program executes on a complexity of O(n^{2}) whereas the efficient approach is comparatively more optimized and the time complexity is O(nlogn).
Next TopicShift Operators in Java
