Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

Minimum number of platforms

Given the arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits.

Algorithm

Code

int findPlatform(int arr[], int dep[], int n)
{
    sort(arr, arr + n);
    sort(dep, dep + n);

    int pltNeeded = 1, result = 1;
    int i = 1, j = 0;

    while (i < n && j < n) {
        if (arr[i] <= dep[j]) {
            pltNeeded++;
            i++;
        } else if (arr[i] > dep[j]) {
            pltNeeded--;
            j++;
        }
        result = max(result, pltNeeded);
    }

    return result;
}