Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
Time: O(NlgN), sorting
Space: O(1)
public class Solution {
private static final Comparator<Interval> INTERVAL_COMPARATOR =
new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return Integer.compare(i1.start, i2.start);
}
};
public List<Interval> merge(List<Interval> intervals) {
List<Interval> result = new ArrayList<>();
if (intervals == null || intervals.size() == 0) {
return result;
}
// Sort according to the start.
Collections.sort(intervals, INTERVAL_COMPARATOR);
int start = intervals.get(0).start;
int end = intervals.get(0).end;
for (int i = 1; i < intervals.size(); ++i) {
Interval interval = intervals.get(i);
if (interval.start <= end) {
end = Math.max(end, interval.end);
} else {
result.add(new Interval(start, end));
start = interval.start;
end = interval.end;
}
}
result.add(new Interval(start, end));
return result;
}
}