0%
贪心算法
贪心算法模板题
无重叠的子区间
分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { private: static bool cmp(vector<int> a, vector<int> b){ return a[1]<b[1]; } public: int eraseOverlapIntervals(vector<vector<int>>& intervals) { if(intervals.size()==0) return 0; int n = intervals.size(); int ans=0; sort(intervals.begin(),intervals.end(),cmp); int slow=0; for(int i=1;i<n;i++){ if(intervals[slow][1]>intervals[i][0]){ ans++; }else{ slow=i; } } return ans; } };
|