<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p>

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

<pre>
<strong>Input:</strong> intervals = [[1,3],[2,6],[8,10],[15,18]]
<strong>Output:</strong> [[1,6],[8,10],[15,18]]
<strong>Explanation:</strong> Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
</pre>

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

<pre>
<strong>Input:</strong> intervals = [[1,4],[4,5]]
<strong>Output:</strong> [[1,5]]
<strong>Explanation:</strong> Intervals [1,4] and [4,5] are considered overlapping.
</pre>

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

<ul>
	<li><code>1 &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>4</sup></code></li>
</ul>

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if(intervals.size() == 1){
            return intervals;
        }
         vector<vector<int>> ans;
         for(int i = 0 ;i< intervals.size(); i++){
             for(int j = 0 ;j< intervals.size(); j++){
                if(intervals[i][0] < intervals[j][0]){
                    swap(intervals[i], intervals[j]);
                }
         }
         }
         int idx = 0;
          ans.push_back(intervals[0]);
          for(int i = 1 ;i< intervals.size(); i++){
            auto current = intervals[i];
            auto previous = ans.back();
            if(previous[1] < current[0]){
                ans.push_back(current);
                
            }else{
                ans.back()[1] = max(current[1], previous[1]);
            }
          }

        //    if(previous[1] < current[0]){
        //         ans.push_back(current);
        //     }else{
        //         previous[1] = max(previous[1], current[1]);
        //     }

        /*
         vector<vector<int>> ans;
        ans.push_back(intervals[0]);
        for(int index = 1; index < intervals.size(); index++){
            auto interval = intervals[index];
            //[1,4][2,3] 
            //           |4|.           [2]
            if(ans.back()[1] < interval[0]){
                ans.push_back(interval);
            }else{
                ans.back()[1] = max(ans.back()[1], interval[1]);
            }
        }
        
        */
         return ans;
    }
};
25000