forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Intervals.cpp
25 lines (24 loc) · 967 Bytes
/
Merge Intervals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool mycomp(Interval i1,Interval i2){
return i1.start<i2.start;
}
vector<Interval> Solution::insert(vector<Interval> &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
vector<Interval> sol;
for(Interval i:intervals){
long t1,t2,found=0;
if(newInterval.start>=i.start && newInterval.start<=i.end)
{newInterval.start=i.start;found=1;}
if(newInterval.end<=i.end && newInterval.end>=i.start)
{newInterval.end=i.end;found=1;}
if(i.start>=newInterval.start && i.end<=newInterval.end)
found=1;
if(found==0)
sol.push_back(i);
}
sol.push_back(newInterval);
sort(sol.begin(),sol.end(),mycomp);
return sol;
}