HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Trains scheduled, find max platforms per stations

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pertrainsmaxplatformsfindstationsscheduled

Problem

Given a "folder" which contains many "files," each file contains a time-table of trains, which stations they halt on and what time, per day.

File-1 for day-1
Train1    [station1,  ] [station2,  ] 
Train2    [station1, ] [station3, ] 

File-2 for day-2
Train1    [station1,   ] [station2,  ] 
Train2    [station1, ] [station3, ]




Overlapping days are included in the same file.


E.g: If trains halt from 23.00 - 00.30 then this data would be present
in one of the files only.


We ignore timezones and use 00 - 23.59 scale instead of am and pm.


To find how many platforms a station would need given such daily
schedules.


E.g: If Train1 and Train2 both use Station1 from 10 - 12 then
Station1 needs two platforms.


Complexity:


\$O\$ ( file stations (intervals * log intervals) )



-
file - each file in the folder, aka number of days, whose schedule has been given :
constituted by - for (File file : folder.listFiles()) {

-
stations - constituted by dailyStationPlatformCountMap()

  • intervals - sort inside maxOverlappingInterval




Note: I do understand merits of unit testing in separate files. But I deliberately added it to the main method for personal convenience, so I request that you don't consider that in your feedback.

I'm looking for request code review, optimizations and best practices.

```
final class Intervals implements Serializable {
private final int start;
private final int end;

public Intervals (int start, int end) {
this.start = start;
this.end = end;
}

public int getStart() {
return start;
}

public int getEnd() {
return end;
}
}

public final class TrainTimeTable {

private TrainTimeTable() { }

public static Map stationMaxPlatformMap(String folderName) throws IOException, ClassNotFoundException {
File folder = new File(folderName);
final List> stationPlatformCountList = new Ar

Solution

Quick review, but still good coding like usual.

Naming :

int i = 0;  // ctr for start time array
int j = 0;  // ctr for end time array


Isn't it better to call them pointerStartTime and pointerEndTime?

It helps you when you are programming. Take in mind that you get this code and must find a little bug.
You are getting a headache just by always thinking what i and j are

Check for your own faults :

private static void populateAndSort(int[] startTime, int[] endTime, List interval) {
    for (int i = 0; i < interval.size(); i++) {
        startTime[i] = interval.get(i).getStart();
        endTime[i] = interval.get(i).getEnd();
    }
    Arrays.sort(startTime);
    Arrays.sort(endTime);
}


Its a strange set up and you can make a mistake when you call this method, by not setting the length of the array big enough.

At least this you should be doing :

private static void populateAndSort(int[] startTime, int[] endTime, List interval) {
    if ((startTime.length != endTime.length) || (startTime.length != interval.size())) {
        throw new IllegalArgumentException("All sizes must be the same");
    }
    for (int i = 0; i < interval.size(); i++) {
        startTime[i] = interval.get(i).getStart();
        endTime[i] = interval.get(i).getEnd();
    }
    Arrays.sort(startTime);
    Arrays.sort(endTime);
}


To avoid "abuse" of the method you should do the creation of the int[] in your method self :

private static Map getTimesFromIntervals (List intervals>) {


In the javaDoc you tell exactly that the key for startTime[] is starttime and so on.

This is then clear for every programmer that will use that method.

You see that I cut the sorting to from that method.

I prefer a new method for sorting the 2 arrays :

private static void sortIntegerMap (Map timesMap) {
    for (int[] time : timesMap.values()) {
        Arrays.sort(time);
    }
}


Now you have a more generic method that can sort, even more entries in the map is no problem.

Hope you have something from this review.

Code Snippets

int i = 0;  // ctr for start time array
int j = 0;  // ctr for end time array
private static void populateAndSort(int[] startTime, int[] endTime, List<Intervals> interval) {
    for (int i = 0; i < interval.size(); i++) {
        startTime[i] = interval.get(i).getStart();
        endTime[i] = interval.get(i).getEnd();
    }
    Arrays.sort(startTime);
    Arrays.sort(endTime);
}
private static void populateAndSort(int[] startTime, int[] endTime, List<Intervals> interval) {
    if ((startTime.length != endTime.length) || (startTime.length != interval.size())) {
        throw new IllegalArgumentException("All sizes must be the same");
    }
    for (int i = 0; i < interval.size(); i++) {
        startTime[i] = interval.get(i).getStart();
        endTime[i] = interval.get(i).getEnd();
    }
    Arrays.sort(startTime);
    Arrays.sort(endTime);
}
private static Map<String,int[]> getTimesFromIntervals (List<Intervals> intervals>) {
private static void sortIntegerMap (Map<String,int[]> timesMap) {
    for (int[] time : timesMap.values()) {
        Arrays.sort(time);
    }
}

Context

StackExchange Code Review Q#48178, answer score: 2

Revisions (0)

No revisions yet.