Runtime error! Can someone help with this?


#1

bool comp(Interval a,Interval b)
{
return a.start<b.start;
}
vector Solution::merge(vector &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a
sort(A.begin(),A.end(),comp);
for (auto it = A.begin(); it < A.end(); ++it)
{
if(it->end > (it+1)->start)
{
if(it->end<(it+1)->end)
it->end=(it+1)->end;
A.erase(it+1);
}

 }
 return A;

}


#2

watch your for loop you are traversing from A.begin() to A.end() but inside the loop you are accessing (it+1) which throws runtime error


#3

How can i fix this? how to iterate upto last but second element?


#4

change to this it<A.end() -1