*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;
    }
};

 

上一篇:每日一题#6 HTTP协议及报文组成&LeetCode 435. 无重叠区间【贪心】


下一篇:Leetcode 435. 无重叠区间(Java实现 超详细注释!)