Advertisement
Rohit4Pal

Untitled

Apr 5th, 2022
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. bool mySort(vector<int> A,vector<int> B){
  2.    
  3.     return A[0]<B[0] || (A[0]==B[0] && A[1]<B[1]);
  4. }
  5.  
  6. vector<vector<int> > mergeIntervals(vector<vector<int> > &intervals) {
  7.    
  8.     sort(intervals.begin(),intervals.end(),mySort);
  9.     vector<vector<int> > res;
  10.    
  11.     vector<int> A,B;
  12.     A=intervals[0];
  13.     bool flag=false;
  14.    
  15.     for(int i=0;i<intervals.size();i++){
  16.        
  17.         B=intervals[i];
  18.         if(B[0]<=A[1]){
  19.             A[1]=max(A[1],B[1]);
  20.         }else{
  21.             res.push_back(A);
  22.             A=B;
  23.         }
  24.     }
  25.     res.push_back(A);
  26.    
  27.     return res;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement