AC solution C++... need to simplifiy somehow


  • 0
    class Solution {
    public:
        vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
      		vector<Interval> result;
          result.push_back(newInterval);
      		if(intervals.empty()) return result;
      		for (int i = 0; i < intervals.size(); i++)
      		{
      		      if(intervals[i].end < newInterval.start 
                                     || intervals[i].start > newInterval.end)
                    result.push_back(intervals[i]);
                  else 
                  {
                    result[0].start=min(intervals[i].start, result[0].start);
                    result[0].end=max(intervals[i].end, result[0].end);
                  }
                    
      		}      
          sort(result.begin(), result.end(), [](const Interval& A, const Interval& B){return A.start<B.start;});
      		return result; 
        }
    };
    

Log in to reply
 

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