Merge Intervals (medium)
Problem Statement #
Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals.
Example 1:
Intervals: [[1,4], [2,5], [7,9]]
Output: [[1,5], [7,9]]
Explanation: Since the first two intervals [1,4] and [2,5] overlap, we merged them into
one [1,5].
Example 2:
Intervals: [[6,7], [2,4], [5,9]]
Output: [[2,4], [5,9]]
Explanation: Since the intervals [6,7] and [5,9] overlap, we merged them into one [5,9].
Example 3:
Intervals: [[1,4], [2,6], [3,5]]
Output: [[1,6]]
Explanation: Since all the given intervals overlap, we merged them into one.
Try it yourself #
Try solving this question here:
import java.util.*;class Interval {int start;int end;public Interval(int start, int end) {this.start = start;this.end = end;}};class MergeIntervals {public static List<Interval> merge(List<Interval> intervals) {List<Interval> mergedIntervals = new LinkedList<Interval>();// TODO: Write your code herereturn mergedIntervals;}public static void main(String[] args) {List<Interval> input = new ArrayList<Interval>();input.add(new Interval(1, 4));input.add(new Interval(2, 5));input.add(new Interval(7, 9));System.out.print("Merged intervals: ");for (Interval interval : MergeIntervals.merge(input))System.out.print("[" + interval.start + "," + interval.end + "] ");System.out.println();input = new ArrayList<Interval>();input.add(new Interval(6, 7));input.add(new Interval(2, 4));input.add(new Interval(5, 9));System.out.print("Merged intervals: ");for (Interval interval : MergeIntervals.merge(input))System.out.print("[" + interval.start + "," + interval.end + "] ");System.out.println();input = new ArrayList<Interval>();input.add(new Interval(1, 4));input.add(new Interval(2, 6));input.add(new Interval(3, 5));System.out.print("Merged intervals: ");for (Interval interval : MergeIntervals.merge(input))System.out.print("[" + interval.start + "," + interval.end + "] ");System.out.println();}}
Solution #
Let’s take the example of two intervals (‘a’ and ‘b’) such that a.start <= b.start
. There are four possible scenarios:
Our goal is to merge the intervals whenever they overlap. For the above-mentioned three overlapping scenarios (2, 3, and 4), this is how we will merge them:
The diagram above clearly shows a merging approach. Our algorithm will look like this:
- Sort the intervals on the start time to ensure
a.start <= b.start
- If ‘a’ overlaps ‘b’ (i.e.
b.start <= a.end
), we need to