Greedy, fast C++ solution


  • 0
    M
    class Solution {
    public:
        int eraseOverlapIntervals(vector<Interval>& intervals) {
            sort(intervals.begin(), intervals.end(), [](const Interval& A, const Interval& B) { return A.start < B.start; });
            int i = 0, res = 0, last;
            while (i < intervals.size()) {
                last = intervals[i++].end;
                while (i < intervals.size() && intervals[i].start < last) {
                    ++res;
                    last = min(last, intervals[i++].end);
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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