C++ O(nlogn) solution in 7 lines


  • 1
    T
    int eraseOverlapIntervals(vector<Interval>& intervals) {
        sort(intervals.begin(), intervals.end(), [] (Interval i, Interval j) {return i.end < j.end;});
        int res = 0, temp = INT_MIN;
        for (auto i : intervals) {
            if (temp > i.start) res++;
            else if (i.end > temp) temp = i.end;
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.