## Insert Interval

###### DESCRIPTION (credit Leetcode.com)

Given a list of intervals intervals and an interval newInterval, write a function to insert newInterval into a list of existing, non-overlapping, and sorted intervals based on their starting points. The function should ensure that after the new interval is added, the list remains sorted without any overlapping intervals, merging them if needed.

###### EXAMPLES

Input:

`intervals = [[1,3],[6,9]] newInterval = [2,5]`

Output:

`[[1,5],[6,9]]`

Explanation: The new interval [2,5] overlaps with [1,3], so they are merged into [1,5].

Run your code to see results here

## Explanation

We first want to create a new list merged to store the merged intervals we will return at the end.

This solution operates in 3 phases:

- Add all the intervals starting before newInterval to merged.
- Merge all overlapping intervals with newInterval and add that merged interval to merged.
- Add all the intervals starting after newInterval to merged.

##### Phase 1

In this phase, we add all the intervals starting before newInterval to merged. This involves iterating through the intervals list until the current interval no longer ends before newInterval starts (i.e. intervals[i][1] >= newInterval[0]).

def insertIntervals(intervals, newInterval):merged = []i = 0n = len(intervals)while i < n and intervals[i][1] < newInterval[0]:merged.append(intervals[i])i += 1while i < n and intervals[i][0] <= newInterval[1]:newInterval[0] = min(intervals[i][0], newInterval[0])newInterval[1] = max(intervals[i][1], newInterval[1])i += 1merged.append(newInterval)for j in range(i, n):merged.append(intervals[j])return merged

##### Phase 2

In this phase, we merge all the intervals that overlap with newInterval together into a single interval by updating newInterval to be the minimum start and maximum end of all the overlapping intervals. This involves iterating through the intervals list until the current interval starts after newInterval ends (i.e. intervals[i][0] > newInterval[1]). When that condition is met, we add newInterval to merged and move onto phase 3.

def insertIntervals(intervals, newInterval):merged = []i = 0n = len(intervals)while i < n and intervals[i][1] < newInterval[0]:merged.append(intervals[i])i += 1while i < n and intervals[i][0] <= newInterval[1]:newInterval[0] = min(intervals[i][0], newInterval[0])newInterval[1] = max(intervals[i][1], newInterval[1])i += 1merged.append(newInterval)for j in range(i, n):merged.append(intervals[j])return merged

##### Phase 3

Phase 3 involves adding all the intervals starting after newInterval to merged. This involves iterating through the intervals list until the end of the list, and adding each interval to merged.

After completing these 3 phases, we return merged as the final result.

def insertIntervals(intervals, newInterval):merged = []i = 0n = len(intervals)while i < n and intervals[i][1] < newInterval[0]:merged.append(intervals[i])i += 1while i < n and intervals[i][0] <= newInterval[1]:newInterval[0] = min(intervals[i][0], newInterval[0])newInterval[1] = max(intervals[i][1], newInterval[1])i += 1merged.append(newInterval)for j in range(i, n):merged.append(intervals[j])return merged

### Complexity Analysis

**Time Complexity**: O(n) where n is the number of intervals.
**Space Complexity**: O(n) for the merged output array.

## Solution

def insertIntervals(intervals, newInterval):merged = []i = 0n = len(intervals)while i < n and intervals[i][1] < newInterval[0]:merged.append(intervals[i])i += 1while i < n and intervals[i][0] <= newInterval[1]:newInterval[0] = min(intervals[i][0], newInterval[0])newInterval[1] = max(intervals[i][1], newInterval[1])i += 1merged.append(newInterval)for j in range(i, n):merged.append(intervals[j])return merged

Loading comments...