C++ 13 line in place solution


  • 3
    S
        vector<Interval> merge(vector<Interval>& intervals) {
            sort(intervals.begin(),intervals.end(),[ ](Interval l,Interval r){return l.start < r.start;});
            int counter=0;
            for(int i=1;i<intervals.size();i++){
                if(intervals[i].start <= intervals[i-1-counter].end)
                    intervals[i-1-counter].end=max(intervals[i].end,intervals[i-1-counter++].end);
                else
                    intervals[i-counter]=intervals[i];
            }
            for(int x=0;x<counter;x++)
                intervals.pop_back();
            return intervals;
        }
    

Log in to reply
 

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