Skip to content

Commit 78b20f4

Browse files
authored
Day 23 Solution
1 parent f06ee41 commit 78b20f4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/interval-list-intersections/
2+
3+
class Solution {
4+
public int[][] intervalIntersection(int[][] A, int[][] B) {
5+
List<int[]> ans = new ArrayList();
6+
int i = 0, j = 0;
7+
8+
while (i < A.length && j < B.length) {
9+
// Let's check if A[i] intersects B[j].
10+
// lo - the startpoint of the intersection
11+
// hi - the endpoint of the intersection
12+
int lo = Math.max(A[i][0], B[j][0]);
13+
int hi = Math.min(A[i][1], B[j][1]);
14+
if (lo <= hi)
15+
ans.add(new int[]{lo, hi});
16+
17+
// Remove the interval with the smallest endpoint
18+
if (A[i][1] < B[j][1])
19+
i++;
20+
else
21+
j++;
22+
}
23+
24+
return ans.toArray(new int[ans.size()][]);
25+
}
26+
}

0 commit comments

Comments
 (0)