File tree 1 file changed +8
-7
lines changed
scripts/algorithms/I/Interval List Intersections
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 166 ms (Top 33.07%) | Memory: 48.3 MB (Top 80.35%)
1
2
var intervalIntersection = function ( firstList , secondList ) {
2
3
//if a overlap b - a.start >= b.start && a.start <= b.end
3
4
//if b overlap a - b.start >= a.start && b.start <= a.end
4
-
5
+
5
6
//handle empty edge cases
6
7
if ( firstList . length === 0 || secondList . length === 0 ) {
7
8
return [ ] ;
8
9
}
9
-
10
+
10
11
let merged = [ ] ;
11
12
let i = 0 ;
12
13
let j = 0 ;
13
14
while ( i < firstList . length && j < secondList . length ) {
14
15
let a_overlap_b = firstList [ i ] [ 0 ] >= secondList [ j ] [ 0 ] && firstList [ i ] [ 0 ] <= secondList [ j ] [ 1 ] ;
15
16
let b_overlap_a = secondList [ j ] [ 0 ] >= firstList [ i ] [ 0 ] && secondList [ j ] [ 0 ] <= firstList [ i ] [ 1 ] ;
16
-
17
+
17
18
if ( a_overlap_b || b_overlap_a ) {
18
19
let start = Math . max ( firstList [ i ] [ 0 ] , secondList [ j ] [ 0 ] ) ;
19
20
let end = Math . min ( firstList [ i ] [ 1 ] , secondList [ j ] [ 1 ] ) ;
20
21
merged . push ( [ start , end ] ) ;
21
22
}
22
-
23
+
23
24
if ( firstList [ i ] [ 1 ] < secondList [ j ] [ 1 ] ) {
24
25
i ++ ;
25
26
} else {
26
27
j ++ ;
27
28
}
28
29
}
29
-
30
+
30
31
return merged ;
31
-
32
- } ;
32
+
33
+ } ;
You can’t perform that action at this time.
0 commit comments