[VIDEO] Step-by-Step Visualization and Explanation
https://youtu.be/_1jHnaovdpo\n\nThis is a similar question to question #56: Merge Intervals, except that the array of given intervals is already sorted by start
https://youtu.be/_1jHnaovdpo\n\nThis is a similar question to question #56: Merge Intervals, except that the array of given intervals is already sorted by start<sub>i</sub> and we need to insert a new interval in the correct spot.  In both questions, there must be no overlaps and we\'ll have to merge intervals as necessary.\n\nThe first while loop will simply append each interval to our `result` as long as `intervals[i][1] < newInterval[0]`.  In other words, if an interval <i>ends</i> before `newInterval` <i>starts</i>, then it must be located entirely before `newInterval`, so we can just add that inteval to `result`.\n\nOnce that condition becomes false, we know that we may have hit our first overlap. So in the second loop, we\'ll merge intervals as long as `intervals[i][0] <= newInterval[1]`.  In other words, since we already know from the first loop that `intervals[i]` ends <i>after</i> `newInterval` starts, if `intervals[i]` starts <i>before</i> `newInterval` ends, then the two intervals must overlap.\n\nOnce that condition becomes false, we know that all other intervals <i>start</i> after `newInterval` <i>ends</i>.  So we can safely append the rest of the intervals to `result` and return `result`.\n\n# Code\n```\nclass Solution:\n    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:\n        result = []\n        i = 0\n\n        while i < len(intervals) and intervals[i][1] < newInterval[0]:\n            result.append(intervals[i])\n            i += 1\n\n        while i < len(intervals) and intervals[i][0] <= newInterval[1]:\n            newInterval[0] = min(newInterval[0], intervals[i][0])\n            newInterval[1] = max(newInterval[1], intervals[i][1])\n            i += 1\n\n        result.append(newInterval)\n\n        while i < len(intervals):\n            result.append(intervals[i])\n            i += 1\n\n        return result\n```
['Array', 'Python', 'Python3']