Solving Merge Intervals question on InterviewBit https://www.interviewbit.com/problems/merge-intervals/
following is the code:-
/**
-
Definition for an interval.
*struct Interval {
*int start;
*int end;
*Interval() : start(0), end(0) {}
*Interval(int s, int e) : start(s), end(e) {} -
};
*/
vector Solution::insert(vector &intervals, Interval newInterval) {
// 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 doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
if(intervals.size()==0){
intervals.push_back(newInterval);
return intervals;
}Interval tobeAdded = newInterval;
int i=0;
while(i<intervals.size()){
if(intervals[i].end <tobeAdded.start){
i++;
continue;
}
else if(tobeAdded.end < intervals[i].start){
intervals.insert(intervals.begin()+i,tobeAdded);
break;
}
else{
tobeAdded.start = min(tobeAdded.start,intervals[i].start);
tobeAdded.end = max(tobeAdded.end,intervals[i].end);
intervals.erase(intervals.begin()+i);
}
}if(i == intervals.size()){
intervals.insert(intervals.begin()+i,tobeAdded);
}
return intervals;
}
please tell the reason of TLE as according to me it shoould be get submitted