Last active
April 27, 2017 21:40
-
-
Save Onaiplee/3aa73293ba9596a6edd9e9e5651cbf2b to your computer and use it in GitHub Desktop.
Oracle phone
This file contains 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
ArrayList<Interval> getFreeList(long start, long end, ArrayList<ArrayList<Interval>> busyLists) { | |
ArrayList<Interval> result = new ArrayList<>(); | |
if (start >= end) return result; | |
if (busyLists.size() == 0) { | |
result.add(new Interval(start, end)); | |
return result; | |
} | |
ArrayList<Interval> globalList = new ArrayList<>(); | |
for (ArrayList<Interval> list : busyLists) globalList.addAll(list); | |
if (globalList.size() == 0) { | |
result.add(new Interval(start, end)); | |
return result; | |
} | |
Collections.sort(globalList, new IntervalComparator()); | |
long latestSoFar = start; | |
for (int i = 0; i < globalList.size(); ++i) { | |
if (latestSoFar >= end) return result; | |
Interval current = globalList.get(i); | |
if (current.start >= end) return result; | |
if (current.start > latestSoFar) { | |
result.add(new Interval(latestSoFar, Math.min(current.start, end))); | |
} | |
latestSoFar = Math.max(latestSoFar, current.end); | |
} | |
if (latestSoFar < end) result.add(new Interval(latestSoFar, end)); | |
return result; | |
} | |
class IntervalComparator implements Comparator<Interval> { | |
@Override | |
public int compare(Interval i1, Interval i2) { | |
return i1.start == i2.start ? Long.compare(i1.end, i2.end) : Long.compare(i1.start, i2.start); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment