Posted on

Description

Submission

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if(intervals.size() < 2) return intervals;
        
        vector<vector<int>> res;
        
        sort(intervals.begin(), intervals.end(), 
            [](const vector<int>& v1, const vector<int>& v2) {
                return v1[0] < v2[0];
            });
        
        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 *