Minimum Platforms
Intervals and Scheduling DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Minimum Number of Platforms Required for Trains at a Railway Station, Sorting, Two Pointers, Greedy Algorithm, Time Complexity, Space Complexity, Interval Scheduling, space complexity, greedy algorithm, sorting algorithms, intervals, time complexity analysis, two pointer technique, Interval Merging.
Problem Statement Given two arrays representing the 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 waiting. We are given N trains. The arrival time of the $i^{th}$ train is given by arr[i] and the departure time is given by dep[i]. Consider that all time inputs are in 24 hour format (e.g., 900 represents 9:00 AM, 1530 represents 3:30 PM). Note: If a train arrives at the same time another train departs, they need different platforms. The departure must happen after the arrival processing for that time slot. Input Specification The first line contains an integer n, the number of trains. The second line contains an array arr of size n representing arrival times. The third line contains an array dep of size n representing departure times. Output Specification Return a single integer representing the minimum number of platforms required. Constraints 1 ≤ n ≤ 50,000 0000 ≤ arr[i] ≤ dep[i] ≤ 2359 The arrays arr and dep are not necessarily sorted. Time complexity expected: O(N Log N) Space complexity expected: O(1) auxiliary space (excluding input storage) Sample Test Cases Sample Input 1 Sample Output 1 Explanation: Between 09:40 and 12:00, the 2nd train is present. Between 09:50 and 11:20, the 3rd train is present. Between 11:00 and 11:30, the 4th train is present. At time 11:00, the 2nd, 3rd, and 4th trains are all at the station simultaneously. Therefore, a minimum of 3 platforms are required. Sample Input 2 Sample Output 2 Explanation: Train 1 departs at 10:00. Train 2 arrives at 11:00 (Platform is free). Train 2 departs at 12:00. Train 3 arrives at 12:35 (Platform is free). Only 1 platform is needed as there are no overlapping intervals. Difficulty Level Medium