Please help me get rid of Time limit EXCEEDED!

interview-questions
Tags: #<Tag:0x00007f2424ca5310>

#1

//QUESTION :MERGE OVERLAPPING INTERVAL
/**

  • Definition for an interval.
  • struct Interval {
  • int start;
    
  • int end;
    
  • Interval() : start(0), end(0) {}
    
  • Interval(int s, int e) : start(s), end(e) {}
    
  • };
    */

int mysort(Interval x ,Interval y){
return y.start>x.start ? 1:0 ;
}
vector Solution::merge(vector &A) {

sort(A.begin() ,A.end() ,mysort) ;
for(int  i =0 ;i< A.size()-1 ;i++){
    
    if(A[i].start <=A[i+1].start and A[i].end >=A[i+1].start ){
         
        if(A[i].end <A[i+1].end){
            A[i].end =A[i+1].end ;
        } 
        
        A.erase(A.begin()+i+1) ;
        i-- ;   
    }
}
return A ;

}