Find Minimum  Platforms Required for Railway or Bus Station

package org.wesome.dsalgo;

import java.util.Arrays;

public class CalculateRequiredPlatform {
    static int calculateRequiredPlatform(double arrivalSchedule[], double departureSchedule[]) {
        if (arrivalSchedule.length != departureSchedule.length) {
            System.out.println("invalid schedule");
            return -1;
        }
        int scheduleLength = arrivalSchedule.length;
        Arrays.sort(arrivalSchedule);
        Arrays.sort(departureSchedule);
        int platformForCurrentIteration = 1, totalPlatform = 1;
        int arrivalIndx = 1, j = 0;
        while (arrivalIndx < scheduleLength && j < scheduleLength) {
            if (arrivalSchedule[arrivalIndx] <= departureSchedule[j]) {
                platformForCurrentIteration++;
                arrivalIndx++;
            } else if (arrivalSchedule[arrivalIndx] > departureSchedule[j]) {
                platformForCurrentIteration--;
                j++;
            }
            if (platformForCurrentIteration > totalPlatform) {
                totalPlatform = platformForCurrentIteration;
            }
        }
        return totalPlatform;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.CalculateRequiredPlatform.calculateRequiredPlatform;

public class CalculateRequiredPlatformTest {
    @Test
    void calculateRequiredPlatformTest1() {
        double[] arrivalSchedule = new double[]{9.00, 9.40, 9.50, 11.00, 15.00, 18.00};
        double[] departureSchedule = new double[]{9.10, 12.00, 11.20, 11.30, 19.00, 20.00};
        Assertions.assertEquals(3, calculateRequiredPlatform(arrivalSchedule, departureSchedule));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on