Poison

986. Interval List Intersections

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
List<int[]> list = new ArrayList<>();

int i = 0, j = 0;
while (i < firstList.length && j < secondList.length) {
if (firstList[i][1] < secondList[j][0]) {
i++;
} else if (secondList[j][1] < firstList[i][0]) {
j++;
} else {
list.add(new int[]{Math.max(firstList[i][0], secondList[j][0]), Math.min(firstList[i][1], secondList[j][1])});
if (firstList[i][1] < secondList[j][1]) {
i++;
} else {
j++;
}
}
}

return list.toArray(new int[0][]);
}
}
Reference

986. Interval List Intersections