|
| 1 | +# Insert Interval |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [starti, endi]` represent the start and the end of the `ith` interval and `intervals` is sorted in ascending order by `starti`. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval. |
| 6 | + |
| 7 | +Insert `newInterval` into `intervals` such that the intervals are still sorted in ascending order by `starti` and `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary). |
| 8 | + |
| 9 | +Return `intervals` after the insertion. |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | +``` |
| 15 | +Input: intervals = [[1,3],[6,9]], newInterval = [2,5] |
| 16 | +Output: [[1,5],[6,9]] |
| 17 | +``` |
| 18 | + |
| 19 | +## Approach |
| 20 | + |
| 21 | +### Method 1: Three Pass Algorithm (Recommended) |
| 22 | +1. First pass: Add all intervals before newInterval |
| 23 | +2. Second pass: Merge overlapping intervals with newInterval |
| 24 | +3. Third pass: Add all intervals after newInterval |
| 25 | +4. Most efficient approach |
| 26 | + |
| 27 | +**Time Complexity:** O(n) - Three passes |
| 28 | +**Space Complexity:** O(1) - In-place modification |
| 29 | + |
| 30 | +### Method 2: Binary Search + Merge |
| 31 | +1. Use binary search to find insertion point |
| 32 | +2. Merge overlapping intervals |
| 33 | +3. Less efficient than three pass approach |
| 34 | + |
| 35 | +**Time Complexity:** O(n) - Merge operation |
| 36 | +**Space Complexity:** O(1) - In-place modification |
| 37 | + |
| 38 | +## Algorithm |
| 39 | + |
| 40 | +``` |
| 41 | +1. Initialize result = [] |
| 42 | +2. Add all intervals before newInterval |
| 43 | +3. Merge overlapping intervals with newInterval |
| 44 | +4. Add all intervals after newInterval |
| 45 | +5. Return result |
| 46 | +``` |
| 47 | + |
| 48 | +## Key Insights |
| 49 | + |
| 50 | +- **Three Passes**: Separate intervals into three categories |
| 51 | +- **Local Optimum**: Merge overlapping intervals optimally |
| 52 | +- **Global Optimum**: Maintain sorted order without overlaps |
| 53 | +- **Space Optimization**: Use only necessary space |
| 54 | + |
| 55 | +## Alternative Approaches |
| 56 | + |
| 57 | +1. **Binary Search**: Use binary search for insertion point |
| 58 | +2. **Single Pass**: Use single pass with complex logic |
| 59 | +3. **Sorting**: Sort all intervals and merge |
| 60 | + |
| 61 | +## Edge Cases |
| 62 | + |
| 63 | +- Empty intervals: Return [newInterval] |
| 64 | +- Single interval: Merge if overlapping |
| 65 | +- No overlaps: Insert in correct position |
| 66 | +- All overlaps: Merge into single interval |
| 67 | + |
| 68 | +## Applications |
| 69 | + |
| 70 | +- Interval algorithms |
| 71 | +- Scheduling problems |
| 72 | +- Algorithm design patterns |
| 73 | +- Interview preparation |
| 74 | +- System design |
| 75 | + |
| 76 | +## Optimization Opportunities |
| 77 | + |
| 78 | +- **Three Pass Algorithm**: Most efficient approach |
| 79 | +- **Space Optimization**: O(1) space complexity |
| 80 | +- **Linear Time**: O(n) time complexity |
| 81 | +- **No Extra Space**: Use only necessary space |
0 commit comments