<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) {
        int n=intervals.size();
        vector<vector<int> > arr ;

        sort(intervals.begin(),intervals.end());

        priority_queue<pair<int,int > > pq;

        for(int i=0;i<n;i++)
        {
            int start =intervals[i][0];
            int end   =intervals[i][1];

            if(pq.empty())
            {
                 pq.push(make_pair(start,end));
            }
            else{
                int st=pq.top().first;
                int e=pq.top().second;

                if(start<=e)
                {
                    pq.pop();
                    pq.push(make_pair(st,max(end,e)));
                }else{
                    pq.push(make_pair(start,end));
                }
            }
        }

        while(!pq.empty())
        {
            int start = pq.top().first;
            int end =pq.top().second;

            vector<int> t;

            t.push_back(start);
            t.push_back(end);

            arr.push_back(t);

            pq.pop();

        }
        reverse(arr.begin(),arr.end());

        return arr;


        
    }
};
24400