-
Notifications
You must be signed in to change notification settings - Fork 25
/
Merge Intervals.cpp
49 lines (49 loc) · 1.14 KB
/
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval) {
int i;
vector<Interval> A;
for(i=0;i<intervals.size();i++)
{
if(newInterval.start>intervals[i].start && newInterval.start>intervals[i].end)
{
A.push_back(intervals[i]);
}
else
break;
}
Interval s;
if(i!=intervals.size() && intervals[i].start<newInterval.start )
s.start=intervals[i].start;
else if(i!=intervals.size())
s.start=newInterval.start;
if(i==intervals.size())
{ s.start=newInterval.start;
s.end=newInterval.end;
}
int f=0;
while(i<intervals.size())
{
if(newInterval.end>=intervals[i].start && newInterval.end<intervals[i].end)
{
s.end=intervals[i].end;
i++;
f=1;
break;
}
else if(newInterval.end<intervals[i].start )
{
s.end=newInterval.end;
f=1;
break;
}
i++;
}
if(f==0)
s.end=newInterval.end;
A.push_back(s);
while(i<intervals.size())
{
A.push_back(intervals[i]);
i++;
}
return A;
}