There was an error while loading. Please reload this page.
1 parent f06ee41 commit 78b20f4Copy full SHA for 78b20f4
May LeetCoding Challenge/Day_ 23/Solution.java
@@ -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