1
// Time Complexity O(Nlog(N)) - N is the number of intervals2
// Space Complexity O(N) - N is the number of intervals, can be reduced to O(1) if needed4
public int intersectionSizeTwo(int[][] intervals) {5
// corner case: can intervals be null or empty? No7
// First, sort the intervals by end, then by reverse order start10
new Comparator<int[]>() {12
public int compare(int[] a, int[] b) {20
// Second, for each two intervals, greedily find if the previous interval would satisfy next23
new ArrayList<>(); // basically the ending set S, btw, we actually do not need this but I24
// use it here for better intuition26
// add last two nums within the range27
list.add(intervals[0][1] - 1);28
list.add(intervals[0][1]);30
for (int i = 1; i < intervals.length; i++) {31
int lastOne = list.get(list.size() - 1);32
int lastTwo = list.get(list.size() - 2);34
int[] interval = intervals[i];35
int start = interval[0];36
int end = interval[1];38
// if overlaps at least 239
if (lastOne >= start && lastTwo >= start) {41
} else if (lastOne >= start) { // if overlaps 143
} else { // if not overlapping