435_Non overlappingIntervals - a920604a/leetcode GitHub Wiki


title: 435. Non-overlapping Intervals tags:
- greedy categories: leetcode comments: false

solution

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b){
            return a[1]<b[1];
        });
        int count = 1, n = intervals.size();
        int end = intervals[0][1];
        for(int i=1;i<n;++i){
            vector<int> & cur = intervals[i];
            // ä¸é‡į–ŠīŧŒæŗ¨æ„é€™čŖĄį­‰æ–ŧįŽ—ä¸åŒå€é–“
            if(end<=cur[0]){
                end = cur[1];
                count++;
            }
        }
        return n - count;
    }
};

analysis

  • time complexity O(nlogn)
  • speed complexity O(1)
âš ī¸ **GitHub.com Fallback** âš ī¸