@@ -32,10 +32,29 @@ public class IntervalListIntersections {
3232
3333 public static int [][] intervalIntersection (int [][] firstList , int [][] secondList ) {
3434 List <Point > points = new ArrayList <>();
35- populatePointList (firstList , points , 1 );
36- populatePointList (secondList , points , 2 );
37- points .sort (Comparator .comparing (Point ::x ).thenComparingInt (o -> o .type .ordinal ()));
35+ populatePointList (firstList , points );
36+ populatePointList (secondList , points );
37+ points .sort (Comparator .comparing (Point ::x )
38+ .thenComparingInt (o -> o .type .ordinal ()));
3839
40+ List <Pair > intersections = determinePairs (points );
41+
42+ int [][] result = new int [intersections .size ()][2 ];
43+ for (int i = 0 ; i < intersections .size (); i ++) {
44+ result [i ][0 ] = intersections .get (i ).x1 ;
45+ result [i ][1 ] = intersections .get (i ).x2 ;
46+ }
47+ return result ;
48+ }
49+
50+ private static void populatePointList (int [][] intervals , List <Point > points ) {
51+ for (var pair : intervals ) {
52+ points .add (new Point (pair [0 ], Point .Type .START ));
53+ points .add (new Point (pair [1 ], Point .Type .END ));
54+ }
55+ }
56+
57+ private static List <Pair > determinePairs (List <Point > points ) {
3958 List <Pair > intersections = new ArrayList <>();
4059 var startsAmount = 0 ;
4160 var x = 0 ;
@@ -56,23 +75,10 @@ public static int[][] intervalIntersection(int[][] firstList, int[][] secondList
5675 }
5776 }
5877 }
59-
60- int [][] result = new int [intersections .size ()][2 ];
61- for (int i = 0 ; i < intersections .size (); i ++) {
62- result [i ][0 ] = intersections .get (i ).x1 ;
63- result [i ][1 ] = intersections .get (i ).x2 ;
64- }
65- return result ;
66- }
67-
68- private static void populatePointList (int [][] firstList , List <Point > points , int listNumber ) {
69- for (var pair : firstList ) {
70- points .add (new Point (pair [0 ], Point .Type .START , listNumber ));
71- points .add (new Point (pair [1 ], Point .Type .END , listNumber ));
72- }
78+ return intersections ;
7379 }
7480
75- public record Point (int x , Type type , int listNumber ) {
81+ public record Point (int x , Type type ) {
7682
7783 public enum Type {
7884 START , END
0 commit comments