-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.cpp
50 lines (44 loc) · 1.17 KB
/
insert.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
50
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::max;
using std::min;
// Definition for an interval.
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
void printInfo(){
cout << "[" << start << "," << end << "]" << endl;
}
};
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval){
vector<Interval> res;
auto it = intervals.begin();
for(; it != intervals.end(); it++){
if((*it).start > newInterval.end) break;
else if((*it).end < newInterval.start) res.push_back(*it);
else {
newInterval.start = min((*it).start, newInterval.start);
newInterval.end = max((*it).end, newInterval.end);
}
}
res.push_back(newInterval);
while(it != intervals.end()){
res.push_back(*it);
it++;
}
return res;
}
int main(){
vector<Interval> test = {Interval(1,3), Interval(6,9)};
Interval newInterval(2,5);
vector<Interval> results = insert(test, newInterval);
for(auto i : results){
i.printInfo();
}
return 0;
}