|
| 1 | +# Understanding the Merge Intervals Concept |
| 2 | + |
| 3 | +Think of Merge Intervals like booking meetings in a conference room on a calendar. If you have one meeting from 1:00 PM to 3:00 PM and another meeting from 2:00 PM to 4:00 PM, they overlap. Instead of keeping them separate, you combine them into a single blocked-out block from 1:00 PM to 4:00 PM. |
| 4 | + |
| 5 | +This technique involves dealing with sets of overlapping intervals. The trick is to **sort the intervals** by their starting points first. Once sorted, any overlapping intervals will be right next to each other, allowing you to merge them in a single pass. |
| 6 | + |
| 7 | +There are two primary operations: |
| 8 | +1. **Merging Overlapping Intervals** (Combining intervals that intersect). |
| 9 | +2. **Finding Intersecting Intervals** (Finding common overlaps between lists). |
| 10 | + |
| 11 | +## Type 1: Merging Overlapping Intervals |
| 12 | +First, we sort the array of intervals based on their start values. Then, we take the first interval and compare its end value with the next interval's start value. If they overlap, we merge them by taking the maximum end value. |
| 13 | + |
| 14 | +### The Process Step-by-Step |
| 15 | +**Goal:** Merge the intervals `[[1, 3], [2, 6], [8, 10], [15, 18]]`. |
| 16 | + |
| 17 | +```plaintext |
| 18 | +Data: [[1, 3], [2, 6], [8, 10], [15, 18]] |
| 19 | +(Already sorted by start times: 1, 2, 8, 15) |
| 20 | +
|
| 21 | +Step 1: Current = [1, 3] |
| 22 | + Compare with [2, 6]. |
| 23 | + Does 2 <= 3? Yes! Overlap exists. |
| 24 | + Merge: Start = 1, End = max(3, 6) = 6. |
| 25 | + New Current = [1, 6] |
| 26 | +
|
| 27 | +Step 2: Current = [1, 6] |
| 28 | + Compare with [8, 10]. |
| 29 | + Does 8 <= 6? No! No overlap. |
| 30 | + Push [1, 6] to Result. |
| 31 | + New Current = [8, 10] |
| 32 | +
|
| 33 | +Step 3: Current = [8, 10] |
| 34 | + Compare with [15, 18]. |
| 35 | + Does 15 <= 10? No! No overlap. |
| 36 | + Push [8, 10] to Result. |
| 37 | + New Current = [15, 18] |
| 38 | +
|
| 39 | +Step 4: End of list. Push final Current [15, 18] to Result. |
| 40 | +
|
| 41 | +Result: [[1, 6], [8, 10], [15, 18]] |
| 42 | +``` |
| 43 | + |
| 44 | +## Type 2: Inserting a New Interval |
| 45 | +When inserting a new interval into a list of already non-overlapping intervals, you iterate through the list. You add intervals that come before the new one, merge the overlapping ones by updating the new interval's bounds, and then add the remaining intervals. |
| 46 | + |
| 47 | +### The Process Step-by-Step |
| 48 | +**Goal:** Insert `[4, 8]` into `[[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]]`. |
| 49 | + |
| 50 | +```plaintext |
| 51 | +New Interval: [4, 8] |
| 52 | +Data: [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]] |
| 53 | +
|
| 54 | +Step 1: Compare [1, 2] with [4, 8]. Ends before 4. |
| 55 | + Add [1, 2] to Result. |
| 56 | +
|
| 57 | +Step 2: Compare [3, 5] with [4, 8]. Overlaps! |
| 58 | + Merge: [min(3, 4), max(5, 8)] = [3, 8]. |
| 59 | + New Interval is now [3, 8]. |
| 60 | +
|
| 61 | +Step 3: Compare [6, 7] with [3, 8]. Overlaps! |
| 62 | + Merge: [min(6, 3), max(7, 8)] = [3, 8]. |
| 63 | + New Interval remains [3, 8]. |
| 64 | +
|
| 65 | +Step 4: Compare [8, 10] with [3, 8]. Overlaps! |
| 66 | + Merge: [min(8, 3), max(10, 8)] = [3, 10]. |
| 67 | + New Interval is now [3, 10]. |
| 68 | +
|
| 69 | +Step 5: Compare [12, 16] with [3, 10]. Starts after 10. |
| 70 | + Add merged New Interval [3, 10] to Result. |
| 71 | + Add [12, 16] to Result. |
| 72 | +
|
| 73 | +Result: [[1, 2], [3, 10], [12, 16]] |
| 74 | +``` |
| 75 | + |
| 76 | +### Comparison Summary |
| 77 | +| Feature | Merging All Overlaps | Inserting New Interval | |
| 78 | +|---------|----------------------|------------------------| |
| 79 | +| **Prerequisite** | Must sort intervals by start time. | Existing list is already sorted. | |
| 80 | +| **Logic** | `next.start <= current.end` | `current.end < new.start` or `current.start > new.end` | |
| 81 | +| **Used For...** | Consolidating time slots, finding free time. | Adding appointments, dynamic scheduling. | |
0 commit comments