Skip to content

Commit b24666b

Browse files
author
hieu
committed
Solve Problem 986 - Interval List Intersection
1 parent b490caa commit b24666b

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/contest/ReadMe.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class ReadMe {
1414
// greedy: 1354[!] 1353[x] 1338[v]
1515
// stack: 1190[!] 975[x] 1381[v]
1616
// binary search: 1351[v] 1337[v] 1292[!]
17-
// 2 pointers: 1248[*] 1234[v] 986
18-
// sliding window: 1208[v] 1040[!] 995
17+
// 2 pointers: 1248[*] 1234[v] 986[v]
18+
// sliding window: 1208[v] 1040[!] 995[!]
1919
// linked-list: 1290[v] 1019[!] 876
2020
// union-find: 1202[v] 924[v] 947
2121
// heap: 1054[!] 882[!] 864
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package intervals;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Problem986_IntervalListIntersection {
7+
public int[][] intervalIntersection(int[][] A, int[][] B) {
8+
if(A == null || A.length == 0 || B == null || B.length == 0)
9+
return new int[][]{};
10+
List<int[]> res = new ArrayList<>();
11+
12+
int i = 0, j = 0;
13+
int startMax, endMin;
14+
while(i < A.length && j < B.length){
15+
startMax = Math.max(A[i][0], B[j][0]);
16+
endMin = Math.min(A[i][1], B[j][1]);
17+
18+
if(endMin >= startMax)
19+
res.add(new int[]{startMax, endMin});
20+
21+
if(A[i][1] == endMin) i++;
22+
if(B[j][1] == endMin) j++;
23+
}
24+
25+
return res.toArray(new int[res.size()][2]);
26+
}
27+
}

src/intervals/ReadMe.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package intervals;
2+
3+
public class ReadMe {
4+
5+
// Merge 2 intervals:
6+
7+
// [a0, a1]
8+
// [b0, b1]
9+
10+
/*
11+
int startMax = Math.max(a0, b0);
12+
int endMin = Math.min(a1, b1);
13+
14+
if (endMin >= startMax) res.add(new int[] {startMax, endMin});
15+
16+
if (a1 == endMin); // ... increase a
17+
if (b1 == endMin); // ... increase b
18+
*/
19+
20+
}

0 commit comments

Comments
 (0)