Skip to content

Commit 02a0af1

Browse files
authored
Merge pull request #57 from Jer3myYu/java-solutions-intervals
Java Chapter 9: Intervals
2 parents 5ef5c80 + 5af9a90 commit 02a0af1

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
import DS.Interval;
5+
6+
/*
7+
// Definition of Interval:
8+
class Interval {
9+
public int start;
10+
public int end;
11+
public Interval(int start, int end) {
12+
this.start = start;
13+
this.end = end;
14+
}
15+
}
16+
*/
17+
18+
public class IdentifyAllIntervalOverlaps {
19+
public List<Interval> identifyAllIntervalOverlaps(List<Interval> intervals1, List<Interval> intervals2) {
20+
List<Interval> overlaps = new ArrayList<>();
21+
int i, j;
22+
i = j = 0;
23+
while (i < intervals1.size() && j < intervals2.size()) {
24+
// Set A to the interval that starts first and B to the other
25+
// interval.
26+
Interval A, B;
27+
if (intervals1.get(i).start <= intervals2.get(j).start) {
28+
A = intervals1.get(i);
29+
B = intervals2.get(j);
30+
} else {
31+
A = intervals2.get(j);
32+
B = intervals1.get(i);
33+
}
34+
// If there's an overlap, add the overlap.
35+
if (A.end >= B.start) {
36+
overlaps.add(new Interval(B.start, Math.min(A.end, B.end)));
37+
}
38+
// Advance the pointer associated with the interval that ends
39+
// first.
40+
if (intervals1.get(i).end < intervals2.get(j).end) {
41+
i++;
42+
} else {
43+
j++;
44+
}
45+
}
46+
return overlaps;
47+
}
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
import DS.Interval;
5+
6+
/*
7+
// Definition of Interval:
8+
class Interval {
9+
public int start;
10+
public int end;
11+
public Interval(int start, int end) {
12+
this.start = start;
13+
this.end = end;
14+
}
15+
}
16+
*/
17+
18+
public class LargestOverlapOfIntervals {
19+
public int largestOverlapOfIntervals(List<Interval> intervals) {
20+
List<int[]> points = new ArrayList<>();
21+
for (Interval interval : intervals) {
22+
points.add(new int[]{interval.start, 'S'});
23+
points.add(new int[]{interval.end, 'E'});
24+
}
25+
// Sort in chronological order. If multiple points occur at the same
26+
// time, ensure end points are prioritized before start points.
27+
Collections.sort(points, (a, b) -> {
28+
if (a[0] == b[0]) return Integer.compare(a[1], b[1]);
29+
else return Integer.compare(a[0], b[0]);
30+
});
31+
int activeInterval= 0;
32+
int maxOverlaps = 0;
33+
for (int[] point : points) {
34+
int time = point[0];
35+
int pointType = point[1];
36+
if (pointType == 'S') {
37+
activeInterval++;
38+
} else {
39+
activeInterval--;
40+
}
41+
maxOverlaps = Math.max(maxOverlaps, activeInterval);
42+
}
43+
return maxOverlaps;
44+
}
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
import DS.Interval;
6+
7+
/*
8+
// Definition of Interval:
9+
class Interval {
10+
public int start;
11+
public int end;
12+
public Interval(int start, int end) {
13+
this.start = start;
14+
this.end = end;
15+
}
16+
}
17+
*/
18+
19+
public class MergeOverlappingIntervals {
20+
public List<Interval> mergeOverlappingIntervals(List<Interval> intervals) {
21+
Collections.sort(intervals, (a, b) -> Integer.compare(a.start, b.start));
22+
List<Interval> merged = new ArrayList<>();
23+
merged.add(intervals.get(0));
24+
for (int i = 1; i < intervals.size(); i++) {
25+
Interval B = intervals.get(i);
26+
Interval A = merged.get(merged.size() - 1);
27+
// If A and B don't overlap, add B to the merged list.
28+
if (A.end < B.start) {
29+
merged.add(B);
30+
}
31+
// If they do overlap, merge A with B.
32+
else {
33+
A.end = Math.max(A.end, B.end);
34+
}
35+
}
36+
return merged;
37+
}
38+
}

0 commit comments

Comments
 (0)