05. Merge Intervals
Theory
Description
The Merge Intervals technique is commonly used when dealing with a set of intervals, particularly when we need to combine or identify overlapping ranges.
Types of Interval Relationships
When two intervals, \(a = [a_1, a_2]\) and \(b = [b_1, b_2]\), are compared, they can overlap in various ways. There are six common types of relationships:
-
Non-overlapping (Separate):
- Condition: \(a_2 < b_1\) or \(b_2 < a_1\)
- Description: The intervals do not overlap and are entirely separate. No merging is needed.
-
Partial Overlap (b ends after a):
- Condition: \(a_1 \leq b_1 \leq a_2 \leq b_2\)
- Description: The interval \(b\) partially overlaps \(a\), extending beyond it. In this case, \(b\)'s end is after \(a\)'s end.
-
Complete Overlap (a contains b):
- Condition: \(a_1 \leq b_1 \leq b_2 \leq a_2\)
- Description: The interval \(a\) completely contains \(b\). No merging is needed because \(a\) already encompasses \(b\).
-
Partial Overlap (a ends after b):
- Condition: \(b_1 \leq a_1 \leq b_2 \leq a_2\)
- Description: The interval \(a\) partially overlaps \(b\), extending beyond it. Here, \(a\)'s end is after \(b\)'s end.
-
Complete Overlap (b contains a):
- Condition: \(b_1 \leq a_1 \leq a_2 \leq b_2\)
- Description: The interval \(b\) completely contains \(a\). Like the previous case, no merging is needed because \(b\) already includes \(a\).
-
Identical Intervals:
- Condition: \(a_1 = b_1\) and \(a_2 = b_2\)
- Description: The two intervals are exactly the same. No merging needed since they are already identical.
The General Merge Strategy
- Sorting Intervals
The first step is to sort the intervals by their start points (or first elements). This is crucial because once intervals are ordered, we can easily process them in sequence and determine if any two intervals overlap.
Sorting ensures that we only need to look at adjacent intervals to check for overlap, simplifying the merging process. - Merging Intervals
After sorting, you can iterate through the list of intervals and merge overlapping intervals:- Start with the first interval.
- For each subsequent interval:
- If there is no overlap (i.e., the current interval ends before the next one starts), keep both intervals as separate entities.
- If there is an overlap (i.e., the current interval ends after or exactly at the start of the next interval), merge them by extending the end of the current interval to the maximum end value of both intervals.
Practical Example
Imagine you have time slots, and you're tasked with finding the combined time frame of various meetings that might overlap.
Given Intervals: [[1, 3], [2, 6], [15, 18], [8, 10]]
- Sort the intervals:
After sorting by the starting time, the intervals are[[1, 3], [2, 6], [8, 10], [15, 18]]
. - Merge the intervals:
- Compare [1, 3] and [2, 6]: Since they overlap (3 > 2), merge them into [1, 6].
- Compare [1, 6] with [8, 10]: No overlap (6 < 8), so keep both intervals.
- Compare [8, 10] with [15, 18]: No overlap (10 < 15), so keep both intervals.
- Result:
The merged intervals are[[1, 6], [8, 10], [15, 18]]
.
Problems
1. Merge Intervals (Leetcode:56)
Problem Statement
Code and Explaination
This is first approach
This is second approach
2. Insert Interval (Leetcode:57)
Problem Statement
Code and Explaination
This is first approach
This is second approach
3. Merge Two Sorted Lists (Leetcode:21)
Problem Statement
Code and Explaination
This is not merge intervals problem. fix this later
This is second approach
This is Third approach
4. Merge Two Binary Trees (Leetcode:617)
Problem Statement
Code and Explaination
5. Interval List Intersections (Leetcode:986)
Problem Statement
Code and Explaination
This is first approach
This is second approach
6. Single-Threaded CPU (Leetcode:1834)
Problem Statement
Code and Explaination
7. Car Pooling (Leetcode:1094)
Problem Statement
Code and Explaination
This is first approach
This is second approach