Created
October 1, 2015 01:46
-
-
Save adamblank/c773dff4de99b893cec0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class MyApp { | |
protected List<Integer[]> meetings = new ArrayList<>(); | |
public MyApp() { | |
meetings.add(new Integer[] { 3, 5}); | |
meetings.add(new Integer[] { 0, 1}); | |
meetings.add(new Integer[] { 4, 8}); | |
meetings.add(new Integer[] { 10, 12}); | |
meetings.add(new Integer[] { 9, 10}); | |
} | |
public void sortAsc() { | |
Collections.sort(meetings, new Comparator<Integer[]>() { | |
@Override | |
public int compare(Integer[] i1, Integer[] i2) { | |
return i1[0].compareTo(i2[0]); | |
} | |
}); | |
} | |
public Integer[] get(final int index) { | |
return meetings.get(index); | |
} | |
public List<Integer[]> eliminateOverlaps() { | |
final List<Integer[]> noOverlapList = new ArrayList<>(); | |
Integer[] lastMeeting = null; | |
for (Integer[] meeting : meetings) { | |
if (lastMeeting == null) { | |
lastMeeting = meeting; | |
continue; | |
} | |
if (doMeetingsOverlap(lastMeeting, meeting)) { | |
lastMeeting = mergeMeetings(lastMeeting, meeting); | |
} else { | |
noOverlapList.add(lastMeeting); | |
lastMeeting = meeting; | |
} | |
} | |
noOverlapList.add(lastMeeting); | |
return noOverlapList; | |
} | |
public boolean doMeetingsOverlap(final Integer[] meeting1, final Integer[] meeting2) { | |
return meeting1[1] >= meeting2[0]; | |
} | |
public Integer[] mergeMeetings(final Integer[] meeting1, final Integer[] meeting2) { | |
return new Integer[] { meeting1[0], meeting2[1]}; | |
} | |
public static void printMeetings(final List<Integer[]> meetings) { | |
System.out.print("["); | |
for (Integer[] meeting : meetings) { | |
System.out.print(meetingToString(meeting)); | |
} | |
System.out.print("]"); | |
} | |
public static String meetingToString(final Integer[] meeting) { | |
return "(" + meeting[0] + "," + meeting[1] + ")"; | |
} | |
public static void main(String[] args) { | |
MyApp app = new MyApp(); | |
app.sortAsc(); | |
printMeetings(app.eliminateOverlaps()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kinda sloppy... But it works!