File tree 1 file changed +14
-15
lines changed 1 file changed +14
-15
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public int [][] intervalIntersection (int [][] A , int [][] B ) {
2
+ public int [][] intervalIntersection (int [][] firstList , int [][] secondList ) {
3
3
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 });
11
11
}
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 ++;
17
16
}
18
17
}
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 );
21
20
}
22
21
}
You can’t perform that action at this time.
0 commit comments