Skip to content

Java Chapter 9: Intervals #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions java/Intervals/IdentifyAllIntervalOverlaps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;

import DS.Interval;

/*
// Definition of Interval:
class Interval {
public int start;
public int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
*/

public class IdentifyAllIntervalOverlaps {
public List<Interval> identifyAllIntervalOverlaps(List<Interval> intervals1, List<Interval> intervals2) {
List<Interval> overlaps = new ArrayList<>();
int i, j;
i = j = 0;
while (i < intervals1.size() && j < intervals2.size()) {
// Set A to the interval that starts first and B to the other
// interval.
Interval A, B;
if (intervals1.get(i).start <= intervals2.get(j).start) {
A = intervals1.get(i);
B = intervals2.get(j);
} else {
A = intervals2.get(j);
B = intervals1.get(i);
}
// If there's an overlap, add the overlap.
if (A.end >= B.start) {
overlaps.add(new Interval(B.start, Math.min(A.end, B.end)));
}
// Advance the pointer associated with the interval that ends
// first.
if (intervals1.get(i).end < intervals2.get(j).end) {
i++;
} else {
j++;
}
}
return overlaps;
}
}
45 changes: 45 additions & 0 deletions java/Intervals/LargestOverlapOfIntervals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.ArrayList;
import java.util.List;

import DS.Interval;

/*
// Definition of Interval:
class Interval {
public int start;
public int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
*/

public class LargestOverlapOfIntervals {
public int largestOverlapOfIntervals(List<Interval> intervals) {
List<int[]> points = new ArrayList<>();
for (Interval interval : intervals) {
points.add(new int[]{interval.start, 'S'});
points.add(new int[]{interval.end, 'E'});
}
// Sort in chronological order. If multiple points occur at the same
// time, ensure end points are prioritized before start points.
Collections.sort(points, (a, b) -> {
if (a[0] == b[0]) return Integer.compare(a[1], b[1]);
else return Integer.compare(a[0], b[0]);
});
int activeInterval= 0;
int maxOverlaps = 0;
for (int[] point : points) {
int time = point[0];
int pointType = point[1];
if (pointType == 'S') {
activeInterval++;
} else {
activeInterval--;
}
maxOverlaps = Math.max(maxOverlaps, activeInterval);
}
return maxOverlaps;
}
}
38 changes: 38 additions & 0 deletions java/Intervals/MergeOverlappingIntervals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import DS.Interval;

/*
// Definition of Interval:
class Interval {
public int start;
public int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
*/

public class MergeOverlappingIntervals {
public List<Interval> mergeOverlappingIntervals(List<Interval> intervals) {
Collections.sort(intervals, (a, b) -> Integer.compare(a.start, b.start));
List<Interval> merged = new ArrayList<>();
merged.add(intervals.get(0));
for (int i = 1; i < intervals.size(); i++) {
Interval B = intervals.get(i);
Interval A = merged.get(merged.size() - 1);
// If A and B don't overlap, add B to the merged list.
if (A.end < B.start) {
merged.add(B);
}
// If they do overlap, merge A with B.
else {
A.end = Math.max(A.end, B.end);
}
}
return merged;
}
}