Given arrival and departure times of all trains that reach a railway station. Your task is to find the minimum number of platforms required for the railway station so that no train waits.
Note: Consider that all the trains arrive on the same day and leave on the same day. Also, arrival and departure times must not be same for a train.
Input:
Note: Consider that all the trains arrive on the same day and leave on the same day. Also, arrival and departure times must not be same for a train.
Input:
For each test case, first line will contain an integer N, the number of trains. Next two lines will consist of N space separated time intervals denoting arrival and departure times respectively.
Note: Time intervals are in the 24-hour format(hhmm), preceding zeros are insignificant. 200 means 2:00.
Consider the example for better understanding of input.
Note: Time intervals are in the 24-hour format(hhmm), preceding zeros are insignificant. 200 means 2:00.
Consider the example for better understanding of input.
Output:
For each test case, print the minimum number of platforms required for the trains to arrive and depart safely.
For each test case, print the minimum number of platforms required for the trains to arrive and depart safely.
Constraints:
1 <= N <= 1000
1 <= A[i] < D[i] <= 2359
1 <= N <= 1000
1 <= A[i] < D[i] <= 2359
Example:
Input:
6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000
Input:
6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000
Output:
3
3
Explanation:
Testcase 1: Minimum 3 platforms are required to safely arrive and depart all trains.
Testcase 1: Minimum 3 platforms are required to safely arrive and depart all trains.
//JAVA CODE
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int[]arrive= new int[n]; int[]departure = new int[n]; for(int i=0;i<n;i++) arrive[i]=sc.nextInt(); for(int i=0;i<n;i++) departure[i]=sc.nextInt(); Arrays.sort(arrive); Arrays.sort(departure); //PLATFORM ALLOCATION int platform = 1;int limit=0; for(int i=1;i<n;i++){ for(int j=0;j<i;j++){ if(arrive[i]>departure[j] && j>=limit){ if(i>limit) limit++; break; } else if(arrive[i]<departure[j] && j>=limit){ platform++; break; } } } //PRINT System.out.println(platform); } }
Comments
Post a Comment