<p>You are given an array of non-overlapping intervals <code>intervals</code> 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 <code>intervals</code> is sorted in ascending order by <code>start<sub>i</sub></code>. You are also given an interval <code>newInterval = [start, end]</code> that represents the start and end of another interval.</p>

<p>Insert <code>newInterval</code> into <code>intervals</code> such that <code>intervals</code> is still sorted in ascending order by <code>start<sub>i</sub></code> and <code>intervals</code> still does not have any overlapping intervals (merge overlapping intervals if necessary).</p>

<p>Return <code>intervals</code><em> after the insertion</em>.</p>

<p><strong>Note</strong> that you don&#39;t need to modify <code>intervals</code> in-place. You can make a new array and return it.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> intervals = [[1,3],[6,9]], newInterval = [2,5]
<strong>Output:</strong> [[1,5],[6,9]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
<strong>Output:</strong> [[1,2],[3,10],[12,16]]
<strong>Explanation:</strong> Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= intervals.length &lt;= 10<sup>4</sup></code></li>
	<li><code>intervals[i].length == 2</code></li>
	<li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li><code>intervals</code> is sorted by <code>start<sub>i</sub></code> in <strong>ascending</strong> order.</li>
	<li><code>newInterval.length == 2</code></li>
	<li><code>0 &lt;= start &lt;= end &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        int len = intervals.size();
        int leftVal = -1, rightVal = len;

        if (len == 0) {
            return {newInterval};
        }

        if (len == 1) {
            if (newInterval[0] <= intervals[0][1] && newInterval[1] >= intervals[0][0]) {
                return {{min(newInterval[0], intervals[0][0]), max(newInterval[1], intervals[0][1])}};
            } else {
                if (newInterval[0] < intervals[0][0]) {
                    intervals.emplace(intervals.begin(), newInterval);
                } else
                    intervals.push_back(newInterval);
                return intervals;
            }
        }

        int l = -1, r = len, m;
        while (l + 1 != r) {
            m = (l + r + 1) / 2;
            if (intervals[m][0] >= newInterval[0]) 
                r = m;
            else 
                l = m;
        }

        if (l != -1 && newInterval[0] <= intervals[l][1]) {
            newInterval[0] = intervals[l][0];
            leftVal = l;
        } else {
            leftVal = l+1;
            if (l == -1 && newInterval[1] < intervals[0][0]) {
                intervals.emplace(intervals.begin(), newInterval);
                return intervals;
            }
        }

        l = -1, r = len;
        while (l + 1 != r) {
            m = (l + r) / 2;

            if (intervals[m][1] <= newInterval[1]) 
                l = m;
            else 
                r = m;
        }

        if (r != len && newInterval[1] >= intervals[r][0]) {
            newInterval[1] = intervals[r][1];
            rightVal = r;
        } else {
            rightVal = r-1;
            if (r == len && newInterval[0] > intervals[len-1][1]) {
                intervals.push_back(newInterval);
                return intervals;
            }
        }

        if (leftVal == -1 && newInterval[1] < intervals[0][0]) {
            intervals.emplace(intervals.begin(), newInterval);
            return intervals;
        }

        if (rightVal == len && newInterval[0] > intervals[len-1][1]) {
            intervals.push_back(newInterval);
            return intervals;
        }

        if (leftVal == rightVal + 1) {
            intervals.emplace(intervals.begin() + leftVal, newInterval);
            return intervals;
        }

        if (leftVal <= rightVal) {
            intervals[leftVal] = newInterval;
            if (leftVal == rightVal) {
                return intervals;
            }
            intervals.erase(intervals.begin() + leftVal + 1, intervals.begin() + rightVal + 1);
        }

        return intervals;
    }
};
21100