Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Input: [[1,2],[2,3],[3,4],[1,3]]
Output: 1 Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
Input: [[1,2],[1,2],[1,2]] Output: 2
Input: [[1,2],[2,3]]
Output: 0
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
int n=intervals.size();
if(n==1||n==0)return 0;
sort(intervals.begin(),intervals.end(),[](const vector<int> &a , const vector<int> &b)->bool{
return a[0]<b[0];
});
int i=1,ans=0,j=0;
while(i<n)
{
if(intervals[i][0]<intervals[j][1])
{
if(intervals[i][1]<intervals[j][1])
j=i;
++ans;
}else{
j=i;}
++i;
}
return ans;
}
};