56. Merge Intervals

PHOTO EMBED

Mon Feb 20 2023 19:14:08 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(),intervals.end());
        
        int n=intervals.size();
         vector<vector<int>> vns;
        vector<int> v1=intervals[0];
        for(int i=1;i<n;i++)
        {
            if(v1[1]<intervals[i][0])
            {
                vns.push_back(v1);
                v1=intervals[i];
            }
            else 
            {
                v1[1]=max(v1[1],intervals[i][1]);
            }
        }
        vns.push_back(v1);
        return vns;
    }
};
content_copyCOPY

https://leetcode.com/problems/merge-intervals/