...
/Solution: Minimum Number of Platforms Required for TrainStation
Solution: Minimum Number of Platforms Required for TrainStation
This review provides a detailed analysis of the solutions to find the number of platforms required for a train station.
We'll cover the following...
Solution #1: Brute force
Press + to interact
class MinPlatforms {public static int findPlatform(int[] arrival, int[] departure) {int n = arrival.length;int result = 0;int count = 0;for (int index = 0; index < n; index++) {count = 0;for (int i = 1; i < n; i++) {if (arrival[i] >= arrival[index] && arrival[i] <= departure[index]) {count++;}}if (result < count)result = count;}return result;}}class Main{public static void main(String[] args){//Example 1int arrival[] = {900, 940, 950, 1100, 1500, 1800};int departure[] = {910, 1200, 1120, 1130, 1900, 2000};int answer = MinPlatforms.findPlatform(arrival, departure);System.out.println("Minimum Number of Platforms Required for Plan1 = " + answer);System.out.println();// Example 2int arrival1[] = {200, 210, 300, 320, 350, 500};int departure1[] = {230, 240, 320, 430, 400, 520};int answer2 = MinPlatforms.findPlatform(arrival1, departure1);System.out.println("Minimum Number of Platforms Required for Plan2 = " + answer2);}}
Explanation
The problem is to find the maximum number of trains that are at the given railway station at a time. An iterative solution would be to take every interval one by one and find the number of intervals that overlap with it (lines 8-12). Keep track of the maximum number of intervals that overlap, and then return the maximum value ( ...
Access this course and 1400+ top-rated courses and projects.