Skip to content

Commit c73933b

Browse files
committed
Add IntervalListIntersections task solution
1 parent c37314f commit c73933b

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package by.andd3dfx.common;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
/**
8+
* <pre>
9+
* <a href="https://leetcode.com/problems/interval-list-intersections/description/">Task description</a>
10+
*
11+
* You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi]
12+
* and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.
13+
*
14+
* Return the intersection of these two interval lists.
15+
*
16+
* A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
17+
*
18+
* The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval.
19+
* For example, the intersection of [1, 3] and [2, 4] is [2, 3].
20+
*
21+
* Example 1:
22+
* <img src="https://assets.leetcode.com/uploads/2019/01/30/interval1.png"/>
23+
* Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
24+
* Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
25+
*
26+
* Example 2:
27+
* Input: firstList = [[1,3],[5,9]], secondList = []
28+
* Output: []
29+
* </pre>
30+
*/
31+
public class IntervalListIntersections {
32+
33+
public static int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
34+
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()));
38+
39+
List<Pair> intersections = new ArrayList<>();
40+
var startsAmount = 0;
41+
var x = 0;
42+
for (var point : points) {
43+
switch (point.type) {
44+
case START -> {
45+
startsAmount++;
46+
x = point.x;
47+
}
48+
case END -> {
49+
if (startsAmount <= 0) {
50+
throw new IllegalStateException("END going before any START!");
51+
}
52+
if (startsAmount == 2) {
53+
intersections.add(new Pair(x, point.x));
54+
}
55+
startsAmount--;
56+
}
57+
}
58+
}
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+
}
73+
}
74+
75+
public record Point(int x, Type type, int listNumber) {
76+
77+
public enum Type {
78+
START, END
79+
}
80+
}
81+
82+
public record Pair(int x1, int x2) {
83+
84+
}
85+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package by.andd3dfx.common;
2+
3+
import static by.andd3dfx.common.IntervalListIntersections.intervalIntersection;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class IntervalListIntersectionsTest {
9+
10+
@Test
11+
public void testIntervalIntersection() {
12+
assertThat(intervalIntersection(
13+
new int[][]{{0, 2}, {5, 10}, {13, 23}, {24, 25}},
14+
new int[][]{{1, 5}, {8, 12}, {15, 24}, {25, 26}}
15+
)).isEqualTo(new int[][]{{1, 2}, {5, 5}, {8, 10}, {15, 23}, {24, 24}, {25, 25}});
16+
17+
assertThat(intervalIntersection(
18+
new int[][]{{1, 3}, {5, 9}},
19+
new int[][]{}))
20+
.isEqualTo(new int[][]{});
21+
}
22+
}

0 commit comments

Comments
 (0)