|
| 1 | +[](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript) |
| 2 | +[](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork) |
| 3 | + |
| 4 | +## 57\. Insert Interval |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +You are given an array of non-overlapping intervals `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> represent the start and the end of the <code>i<sup>th</sup></code> interval and `intervals` is sorted in ascending order by <code>start<sub>i</sub></code>. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval. |
| 9 | + |
| 10 | +Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by <code>start<sub>i</sub></code> and `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary). |
| 11 | + |
| 12 | +Return `intervals` _after the insertion_. |
| 13 | + |
| 14 | +**Example 1:** |
| 15 | + |
| 16 | +**Input:** intervals = \[\[1,3],[6,9]], newInterval = [2,5] |
| 17 | + |
| 18 | +**Output:** [[1,5],[6,9]] |
| 19 | + |
| 20 | +**Example 2:** |
| 21 | + |
| 22 | +**Input:** intervals = \[\[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] |
| 23 | + |
| 24 | +**Output:** [[1,2],[3,10],[12,16]] |
| 25 | + |
| 26 | +**Explanation:** Because the new interval `[4,8]` overlaps with `[3,5],[6,7],[8,10]`. |
| 27 | + |
| 28 | +**Example 3:** |
| 29 | + |
| 30 | +**Input:** intervals = [], newInterval = [5,7] |
| 31 | + |
| 32 | +**Output:** [[5,7]] |
| 33 | + |
| 34 | +**Example 4:** |
| 35 | + |
| 36 | +**Input:** intervals = \[\[1,5]], newInterval = [2,3] |
| 37 | + |
| 38 | +**Output:** [[1,5]] |
| 39 | + |
| 40 | +**Example 5:** |
| 41 | + |
| 42 | +**Input:** intervals = \[\[1,5]], newInterval = [2,7] |
| 43 | + |
| 44 | +**Output:** [[1,7]] |
| 45 | + |
| 46 | +**Constraints:** |
| 47 | + |
| 48 | +* <code>0 <= intervals.length <= 10<sup>4</sup></code> |
| 49 | +* `intervals[i].length == 2` |
| 50 | +* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>5</sup></code> |
| 51 | +* `intervals` is sorted by <code>start<sub>i</sub></code> in **ascending** order. |
| 52 | +* `newInterval.length == 2` |
| 53 | +* <code>0 <= start <= end <= 10<sup>5</sup></code> |
| 54 | + |
| 55 | +## Solution |
| 56 | + |
| 57 | +```typescript |
| 58 | +function insert(intervals: number[][], newInterval: number[]): number[][] { |
| 59 | + const n = intervals.length |
| 60 | + let l = 0 |
| 61 | + let r = n - 1 |
| 62 | + while (l < n && newInterval[0] > intervals[l][1]) { |
| 63 | + l++ |
| 64 | + } |
| 65 | + while (r >= 0 && newInterval[1] < intervals[r][0]) { |
| 66 | + r-- |
| 67 | + } |
| 68 | + const res: number[][] = new Array(l + n - r).fill(0).map(() => new Array(2)) |
| 69 | + for (let i = 0; i < l; i++) { |
| 70 | + res[i] = [...intervals[i]] |
| 71 | + } |
| 72 | + res[l] = [ |
| 73 | + Math.min(newInterval[0], l === n ? newInterval[0] : intervals[l][0]), |
| 74 | + Math.max(newInterval[1], r === -1 ? newInterval[1] : intervals[r][1]), |
| 75 | + ] |
| 76 | + for (let i = l + 1, j = r + 1; j < n; i++, j++) { |
| 77 | + res[i] = intervals[j] |
| 78 | + } |
| 79 | + return res |
| 80 | +} |
| 81 | + |
| 82 | +export { insert } |
| 83 | +``` |
0 commit comments