Skip to content

Commit 80d54ce

Browse files
authored
Update Interval List Intersections.java
1 parent 582aea8 commit 80d54ce

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
class Solution {
2-
public int[][] intervalIntersection(int[][] A, int[][] B) {
2+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
33
List<int[]> list = new ArrayList<>();
4-
int startA = 0;
5-
int startB = 0;
6-
while (startA < A.length && startB < B.length) {
7-
int intervalStart = Math.max(A[startA][0], B[startB][0]);
8-
int intervalEnd = Math.min(A[startA][1], B[startB][1]);
9-
if (intervalStart <= intervalEnd) {
10-
list.add(new int[]{intervalStart, intervalEnd});
4+
int idxOne = 0;
5+
int idxTwo = 0;
6+
while (idxOne < firstList.length && idxTwo < secondList.length) {
7+
int maxStart = Math.max(firstList[idxOne][0], secondList[idxTwo][0]);
8+
int minEnd = Math.min(firstList[idxOne][1], secondList[idxTwo][1]);
9+
if (maxStart <= minEnd) {
10+
list.add(new int[]{maxStart, minEnd});
1111
}
12-
if (A[startA][1] < B[startB][1]) {
13-
startA++;
14-
}
15-
else {
16-
startB++;
12+
if (minEnd == firstList[idxOne][1]) {
13+
idxOne++;
14+
} else {
15+
idxTwo++;
1716
}
1817
}
19-
int[][] ans = new int[list.size()][2];
20-
return list.toArray(ans);
18+
int[][] result = new int[list.size()][2];
19+
return list.toArray(result);
2120
}
2221
}

0 commit comments

Comments
 (0)