Untitled
class Solution { // Function to find the minimum number of platforms required at the railway station static int findPlatform(int[] arr, int[] dep, int n) { // Sort both arrival and departure arrays Arrays.sort(arr); Arrays.sort(dep); int count = 0; // Current number of platforms needed int ans = 0; // Maximum platforms needed int i = 0, j = 0; // Use two pointers to traverse both arrays while (i < n) { // If a train is arriving, increment platform count if (arr[i] <= dep[j]) { count++; ans = Math.max(ans, count); i++; } // If a train departs, decrement platform count else { count--; j++; } } return ans; // Return the maximum number of platforms required } }
Leave a Comment