<p>Given an array of intervals <code>intervals</code> where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, return <em>the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</em>.</p>

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

<pre>
<strong>Input:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> [1,3] can be removed and the rest of the intervals are non-overlapping.
</pre>

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

<pre>
<strong>Input:</strong> intervals = [[1,2],[1,2],[1,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> You need to remove two [1,2] to make the rest of the intervals non-overlapping.
</pre>

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

<pre>
<strong>Input:</strong> intervals = [[1,2],[2,3]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> You don&#39;t need to remove any of the intervals since they&#39;re already non-overlapping.
</pre>

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

<ul>
	<li><code>1 &lt;= intervals.length &lt;= 10<sup>5</sup></code></li>
	<li><code>intervals[i].length == 2</code></li>
	<li><code>-5 * 10<sup>4</sup> &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 5 * 10<sup>4</sup></code></li>
</ul>

 int heapsize;
class Solution {
public:


    void MaxHeapify(vector<vector<int>> &A,int i){

        int l =2*i+1;
        int r =2*i+2;
        int largest=i;

        if(l<heapsize && A[l][1] > A[largest][1]){
            largest=l;
        }

        if(r<heapsize && A[r][1] > A[largest][1]){
            largest = r;
        }

        if(largest != i){
            vector<int> temp = A[largest];
            A[largest]=A[i];
            A[i] = temp;
            MaxHeapify(A,largest);
        }
    }


    void BuildMaxHeap(vector<vector<int>> &A){
        heapsize = A.size();
        for(int i=(A.size()/2)-1;i>=0;i--){
            MaxHeapify(A,i);
        }
    }

    void HeapSort(vector<vector<int>> &A){
        BuildMaxHeap(A);
        for(int i=A.size()-1;i>=1;i--){
            vector<int> temp=A[0];
            A[0] = A[i];
            A[i]=temp;
            heapsize--;
            MaxHeapify(A,0);
        }
    }

    int minDelectionsIntervals(vector<vector<int>> &intervals){

        HeapSort(intervals);

        vector<vector<int>> A;
        A.push_back(intervals[0]);
        int fi=intervals[0][1];

        for(int j=1;j<intervals.size();j++){
            if(intervals[j][0] >= fi){
                fi = intervals[j][1];
                A.push_back(intervals[j]);
            }
        }
        return intervals.size()-A.size();
    }

    int eraseOverlapIntervals(vector<vector<int>>& intervals) {

        return minDelectionsIntervals(intervals);
        /*
        sort(intervals.begin(),intervals.end(),[](const vector<int>& a, const vector<int>& b) {
    return a[1] < b[1];});

        //massimo numero di task che riesco a schedulare
        vector<vector<int>> A;
        A.push_back(intervals[0]);
        int f = intervals[0][1];

        for(int j=1;j<intervals.size();j++){
            if(intervals[j][0] >= f){
                A.push_back(intervals[j]);
                f = intervals[j][1];
            }
        } 

        if(!A.empty()){
            return intervals.size()-A.size();
        }else{
            return intervals.size();
        }
        */
    }
};
210090