|
| 1 | +# Interval List Intersections |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +You are given two lists of closed intervals, `firstList` and `secondList`, where `firstList[i] = [starti, endi]` and `secondList[j] = [startj, endj]`. Each list of intervals is pairwise disjoint and in sorted order. |
| 6 | + |
| 7 | +Return the intersection of these two interval lists. |
| 8 | + |
| 9 | +A closed interval `[a, b]` (with `a <= b`) denotes the set of real numbers `x` with `a <= x <= b`. |
| 10 | + |
| 11 | +The intersection of two closed intervals is a set of real numbers that are empty or represented as a closed interval. For example, the intersection of `[1, 3]` and `[2, 4]` is `[2, 3]`. |
| 12 | + |
| 13 | +## Examples |
| 14 | + |
| 15 | +**Example 1:** |
| 16 | +``` |
| 17 | +Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]] |
| 18 | +Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]] |
| 19 | +``` |
| 20 | + |
| 21 | +## Approach |
| 22 | + |
| 23 | +### Method 1: Two Pointers (Recommended) |
| 24 | +1. Use two pointers to traverse both lists |
| 25 | +2. Find intersection of current intervals |
| 26 | +3. Move pointer of interval that ends first |
| 27 | +4. Most efficient approach |
| 28 | + |
| 29 | +**Time Complexity:** O(m + n) - Two pointers |
| 30 | +**Space Complexity:** O(1) - In-place modification |
| 31 | + |
| 32 | +### Method 2: Brute Force |
| 33 | +1. Check all pairs of intervals |
| 34 | +2. Less efficient than two pointers approach |
| 35 | + |
| 36 | +**Time Complexity:** O(m * n) - Nested loops |
| 37 | +**Space Complexity:** O(1) - No extra space |
| 38 | + |
| 39 | +## Algorithm |
| 40 | + |
| 41 | +``` |
| 42 | +1. Initialize i = 0, j = 0 |
| 43 | +2. While i < m and j < n: |
| 44 | + a. Find intersection of intervals[i] and intervals[j] |
| 45 | + b. If intersection exists: add to result |
| 46 | + c. Move pointer of interval that ends first |
| 47 | +3. Return result |
| 48 | +``` |
| 49 | + |
| 50 | +## Key Insights |
| 51 | + |
| 52 | +- **Two Pointers**: Efficiently traverse both lists |
| 53 | +- **Local Optimum**: Find intersection of current intervals |
| 54 | +- **Global Optimum**: All intersections found |
| 55 | +- **Space Optimization**: Use only necessary space |
| 56 | + |
| 57 | +## Alternative Approaches |
| 58 | + |
| 59 | +1. **Brute Force**: Check all pairs |
| 60 | +2. **Sweep Line**: Use sweep line algorithm |
| 61 | +3. **Binary Search**: Use binary search for efficiency |
| 62 | + |
| 63 | +## Edge Cases |
| 64 | + |
| 65 | +- Empty lists: Return empty list |
| 66 | +- No intersections: Return empty list |
| 67 | +- All intersections: Return all intervals |
| 68 | +- Single intersection: Return that intersection |
| 69 | + |
| 70 | +## Applications |
| 71 | + |
| 72 | +- Interval algorithms |
| 73 | +- Scheduling problems |
| 74 | +- Algorithm design patterns |
| 75 | +- Interview preparation |
| 76 | +- System design |
| 77 | + |
| 78 | +## Optimization Opportunities |
| 79 | + |
| 80 | +- **Two Pointers**: Most efficient approach |
| 81 | +- **Space Optimization**: O(1) space complexity |
| 82 | +- **Linear Time**: O(m + n) time complexity |
| 83 | +- **No Extra Space**: Use only necessary space |
0 commit comments