其他分享
首页 > 其他分享> > *435. 无重叠区间(贪心)

*435. 无重叠区间(贪心)

作者:互联网

### 解题思路

贪心策略:按照右端点从小到大排序,然后拼接区间

### 代码

class Solution {
public:
    static bool cmp(vector<int>& a,vector<int>& b){
         return a[1] < b[1];
    }
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if(intervals.size() == 0) return 0;
        sort(intervals.begin(),intervals.end(),cmp);
        int n = intervals.size();
        int count = 1;
        int last = intervals[0][1];
        for(int i = 1; i < n; ++i){
            if(last <= intervals[i][0]){
                count++;
                last = intervals[i][1];
            }
        }

        return n-count;
    }
};

 

标签:size,vector,return,重叠,int,intervals,last,435,贪心
来源: https://blog.csdn.net/alex1997222/article/details/113832920