Partial correct because time limit exceeded


#1

void merge(vector &A,int p,int q,int r){
int n1=q-p+1;
int n2=r-q;
Interval L[n1+1],R[n2+1];
for(int i=0;i<n1;i++) L[i]=A[p+i-1];
for(int i=0;i<n2;i++) R[i]=A[q+i];
L[n1].start=2147483647;
R[n2].start=2147483647;
int i=0,j=0;
for(int k=p-1;k<r;k++){
if(L[i].start<=R[j].start){
A[k]=L[i];
i++;
}
else{
A[k]=R[j];
j++;
}
}
}
void merge_sort(vector &A,int p,int r){
if(p<r) {
int q=(p+r)/2;
merge_sort(A,p,q);
merge_sort(A,q+1,r);
merge(A,p,q,r);
}
}
vector Solution::merge(vector &A) {
merge_sort(A,1,A.size());
for(int i=1;i<A.size();i++){
if(A[i].start<=A[i-1].end){
if(A[i].end>=A[i-1].end){
A[i-1].end=A[i].end;
}
A.erase(A.begin()+i);
i–;
}
}
return A;
}