From 2a8e5bb69def5e142afd6e9a952656c1187a6ec6 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Wed, 10 Apr 2024 21:59:56 +0200 Subject: [PATCH] [fix-merging-overlapping] fix --- .../MergeOverlappingIntervals.java | 18 +++++------ .../MergeIntervalsUnitTest.java | 32 ++++++++++++------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java b/algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java index ba424772d5..ec7081bec7 100644 --- a/algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java +++ b/algorithms-modules/algorithms-miscellaneous-8/src/main/java/com/baeldung/algorithms/mergeintervals/MergeOverlappingIntervals.java @@ -1,23 +1,22 @@ package com.baeldung.algorithms.mergeintervals; +import static java.lang.Integer.max; + import java.util.ArrayList; +import java.util.Comparator; 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 + intervals.sort(Comparator.comparingInt(interval -> interval.start)); 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); + Interval lastMerged = merged.get(merged.size() - 1); + if (interval.start <= lastMerged.end){ + lastMerged.setEnd(max(interval.end, lastMerged.end)); } else { merged.add(interval); } @@ -25,5 +24,4 @@ public class MergeOverlappingIntervals { return merged; } - -} +} \ No newline at end of file diff --git a/algorithms-modules/algorithms-miscellaneous-8/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java b/algorithms-modules/algorithms-miscellaneous-8/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java index 5f15fe00f6..2d8a94dca4 100644 --- a/algorithms-modules/algorithms-miscellaneous-8/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java +++ b/algorithms-modules/algorithms-miscellaneous-8/src/test/java/com/baeldung/algorithms/mergeintervals/MergeIntervalsUnitTest.java @@ -1,30 +1,38 @@ package com.baeldung.algorithms.mergeintervals; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; 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 List intervals = new ArrayList<>(Arrays.asList( + // @formatter:off + new Interval(3, 5), + new Interval(13, 20), + new Interval(11, 15), + new Interval(15, 16), + new Interval(1, 3), + new Interval(4, 5), + new Interval(16, 17) + // @formatter:on )); - private ArrayList intervalsMerged = new ArrayList<>(Arrays.asList( - new Interval(1, 5), - new Interval(11, 20) + private List intervalsMerged = new ArrayList<>(Arrays.asList( + // @formatter:off + new Interval(1, 5), + new Interval(11, 20) + // @formatter:on )); @Test void givenIntervals_whenMerging_thenReturnMergedIntervals() { MergeOverlappingIntervals merger = new MergeOverlappingIntervals(); - ArrayList result = (ArrayList) merger.doMerge(intervals); - assertArrayEquals(intervalsMerged.toArray(), result.toArray()); + List result = merger.doMerge(intervals); + assertEquals(intervalsMerged, result); } -} +} \ No newline at end of file