Posted on

Description

`

Submission

class Solution {
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        intervals.push_back(newInterval);
        sort(intervals.begin(), intervals.end(), 
            [](const vector<int>& v1, const vector<int>& v2) {
               return v1[0] < v2[0]; 
            });
        vector<vector<int>> res;
         vector<int> curr = intervals[0];
        for(int i = 1; i < intervals.size(); ++i) {
            if(intervals[i][0] <= curr[1]) {
                if(intervals[i][1] <= curr[1]) continue;
                curr[1] = intervals[i][1];
            } else {
                res.push_back(curr);
                curr = intervals[i];
            }
        }
        res.push_back(curr);
        return res;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *