-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path57.cpp
56 lines (48 loc) · 1.8 KB
/
57.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
51
52
53
54
55
56
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int current = 0;
int pos[2] = {-1,-1};
int secPos[2]= {-1, -1};
for (int i = 0 ; i < intervals.size() ; i++){
if (newInterval[current] <= intervals[i][1]){
//cout << newInterval[current] << "test" << endl;
pos[current] = i;
secPos[current] = newInterval[current] < intervals[i][0] ? 0 : 1;
++current;
i-=1;
if(current == 2) break;
}
}
if (pos[0] == -1){
intervals.push_back(newInterval);
return intervals;
}
if (pos[0] != pos[1]){
if(secPos[0] == 0) intervals[pos[0]][0] = newInterval[0];
if(pos[1] == -1){
intervals[pos[0]][1] = newInterval[1];
intervals.erase(intervals.begin() + pos[0] + 1, intervals.end());
}
else{
if (secPos[1] == 0) {
intervals[pos[0]][1] = newInterval[1];
intervals.erase(intervals.begin() + pos[0] + 1, intervals.begin() + pos[1]);
}
else{
intervals[pos[0]][1] = intervals[pos[1]][1];
intervals.erase(intervals.begin() + pos[0] + 1, intervals.begin() + pos[1] + 1);
}
}
}
else{
if (secPos[0] != secPos[1]){
intervals[pos[0]][0] = newInterval[0];
}
else if (secPos[0] == 0){
intervals.insert(intervals.begin() + pos[0], newInterval);
}
}
return intervals;
}
};