Interval List Intersections LeetCode solution in Java

1
class Solution {
2
public int[][] intervalIntersection(int[][] A, int[][] B) {
3
List<int[]> ans = new ArrayList<>();
4
int i = 0, j = 0;
5
while (i < A.length && j < B.length) {
6
int start = Math.max(A[i][0], B[j][0]);
7
int end = Math.min(A[i][1], B[j][1]);
8
if (start <= end) ans.add(new int[] {start, end});
9
if (A[i][1] > B[j][1]) j++;
10
else i++;
11
}
12

13
int[][] res = new int[ans.size()][2];
14
i = 0;
15
for (int[] pair : ans) {
16
res[i++] = pair;
17
}
18

19
return res;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0