From d7527a382e328388ceaf844a0b69f69461e216bc Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Thu, 22 Feb 2024 15:11:26 +0000 Subject: [PATCH 1/3] BAEL-7513 Merge overlapping intervals in collection --- .../algorithms/mergeintervals/Interval.java | 37 +++++++++++++++++++ .../MergeOverlappingIntervals.java | 29 +++++++++++++++ .../MergeIntervalsUnitTest.java | 30 +++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java new file mode 100644 index 0000000000..1b13b8b279 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java @@ -0,0 +1,37 @@ +package com.baeldung.algorithms.mergeintervals; + +import java.util.Objects; + +public class Interval { + int start; + int end; + + Interval(int start, int end) { + this.start = start; + this.end = end; + } + + public void setEnd(int end) { + this.end = end; + } + + @Override + public String toString() { + return "Interval{" + "start=" + start + ", end=" + end + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Interval interval = (Interval) o; + return start == interval.start && end == interval.end; + } + + @Override + public int hashCode() { + return Objects.hash(start, end); + } +} \ No newline at end of file diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java new file mode 100644 index 0000000000..ba424772d5 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java @@ -0,0 +1,29 @@ +package com.baeldung.algorithms.mergeintervals; + +import java.util.ArrayList; +import java.util.List; + +public class MergeOverlappingIntervals { + + public List doMerge(List intervals) { + // Sort the intervals based on start time + intervals.sort((one, two) -> one.start - two.start); + + // Create somewhere to put the merged list, start it off with the earliest starting interval + ArrayList merged = new ArrayList<>(); + merged.add(intervals.get(0)); + + // Loop over each interval and merge if start time is before the end time of the + // previous interval + intervals.forEach(interval -> { + if (merged.get(merged.size() - 1).end > interval.start) { + merged.get(merged.size() - 1).setEnd(interval.end); + } else { + merged.add(interval); + } + }); + + return merged; + } + +} diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java new file mode 100644 index 0000000000..5f15fe00f6 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.algorithms.mergeintervals; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +class MergeIntervalsUnitTest { + + private ArrayList intervals = new ArrayList<>(Arrays.asList( + new Interval(2, 5), + new Interval(13, 20), + new Interval(11, 15), + new Interval(1, 3) + )); + private ArrayList intervalsMerged = new ArrayList<>(Arrays.asList( + new Interval(1, 5), + new Interval(11, 20) + )); + + @Test + void givenIntervals_whenMerging_thenReturnMergedIntervals() { + MergeOverlappingIntervals merger = new MergeOverlappingIntervals(); + ArrayList result = (ArrayList) merger.doMerge(intervals); + assertArrayEquals(intervalsMerged.toArray(), result.toArray()); + } + +} From 433a32489c6e1eb30580cf4b3226b617ba262109 Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Thu, 22 Feb 2024 15:13:15 +0000 Subject: [PATCH 2/3] BAEL-7513 Add missing newline to Interval.java --- .../java/com/baeldung/algorithms/mergeintervals/Interval.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java index 1b13b8b279..f926d578be 100644 --- a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java @@ -34,4 +34,4 @@ public class Interval { public int hashCode() { return Objects.hash(start, end); } -} \ No newline at end of file +} From 9c539cdd3d6421a78a09bde301d797bfe54e2166 Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Thu, 28 Mar 2024 14:41:51 +0000 Subject: [PATCH 3/3] BAEL-7513 Move merge interval code to next module --- .../java/com/baeldung/algorithms/mergeintervals/Interval.java | 0 .../algorithms/mergeintervals/MergeOverlappingIntervals.java | 0 .../algorithms/mergeintervals/MergeIntervalsUnitTest.java | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename algorithms-modules/{algorithms-miscellaneous-7 => algorithms-miscellaneous-8}/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java (100%) rename algorithms-modules/{algorithms-miscellaneous-7 => algorithms-miscellaneous-8}/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java (100%) rename algorithms-modules/{algorithms-miscellaneous-7 => algorithms-miscellaneous-8}/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java (100%) diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java b/algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java similarity index 100% rename from algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java rename to algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/Interval.java diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java b/algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java similarity index 100% rename from algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java rename to algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java b/algorithms-modules/algorithms-miscellaneous-8/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java similarity index 100% rename from algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java rename to algorithms-modules/algorithms-miscellaneous-8/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java