C++ Solution to Non-overlapping Intervals


  • 0
    G
    class Solution {
    private:
        struct comp{
          bool operator()(Interval a, Interval b){
            return a.start<b.start;
          }  
        };
    public:
        int eraseOverlapIntervals(vector<Interval>& intervals) {
            sort(intervals.begin(),intervals.end(),comp());
            int index=INT_MIN;
            int result=0;
            for(int i=0;i<intervals.size();i++){
                if(intervals[i].start<index&&intervals[i].start!=INT_MIN){
                    index=min(index,intervals[i].end);
                    result++;continue;
                }
                index=intervals[i].end;
            }
            return result;
        }
    };

Log in to reply
 

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