<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>

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        //dp
        //sort by end dates
        sort(intervals.begin(), intervals.end(), 
            [&](vector<int>& l,vector<int>& r){
                return l[1]<r[1];
            });

        // map<int, int> times;
        // hmmmm... just do n^2
        int dp [intervals.size()]; //number of intervals to remove
        //that end at intervals[i].end (may be dupes, use the latest)
        dp[0]= 0;
        for(int i=1; i<intervals.size(); i++){
            // cout <<dp[i-1] << endl;

            if(intervals[i-1][1]>intervals[i][0]){//if prev interval overlaps
                //assume delete this
                dp[i] = dp[i-1]+1;
            }else{
                dp[i]=dp[i-1]; //no overlap, just continue
                continue;
            }
           
            //or delete some previous intervals?
            int j= binSearch(i-1, intervals[i][0], intervals);
            if(j>=0)
                dp[i] = min(dp[i], dp[j] + i-j-1);
            // for(int j=i-1; j>=0; j--){
            //     if(intervals[j][1] <= intervals[i][0]){
            //         //interval j, which ends right before i starts
            //         dp[i] = min(dp[i], dp[j] + i-j-1);
            //         break;
            //     }
            // }
        }
        return dp[intervals.size()-1];
    }
    //index j where j<=r and intervals[j][1] <= target
    int binSearch(int r, int target, vector<vector<int>>& intervals){
        if(intervals[0][1]>target)
            return -1;
        //returns first element greater than target
        vector<vector<int>>::iterator j= upper_bound(intervals.begin(), intervals.begin()+(r+1), target, 
            [&](int t, vector<int> it){
                return t < it[1];
            });
        
        //first element greater than target
        return (j-intervals.begin()-1);
    }
};
199638