Easy to Understand C++ Solution


  • 0
    S
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     *     Interval() : start(0), end(0) {}
     *     Interval(int s, int e) : start(s), end(e) {}
     * };
     */
    class Solution {
    public:
        int eraseOverlapIntervals(vector<Interval>& intervals) {
            if(intervals.empty()) return 0;
            sort(intervals.begin(),intervals.end(),[](const Interval& v1,const Interval&v2)->int{
               return v1.start<v2.start; 
            });
            int count = 0;
            int end = intervals[0].end;
            for(int i = 1;i<intervals.size();++i) {
    
                if(intervals[i].start<end) {
                    count++;
                    if(intervals[i].end<end)
                        end = intervals[i].end;
                }
                else {
                    end = intervals[i].end;
                }
            }
            return count;
        }
    };
    

Log in to reply
 

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