forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert_Interval.cc
40 lines (40 loc) · 1.34 KB
/
Insert_Interval.cc
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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<Interval> ans;
int st_pos = 1, end_pos = 0, cur = 0;
while (cur < intervals.size() && intervals[cur].end < newInterval.start) {
ans.push_back(intervals[cur++]);
}
if (intervals.size() == cur) {
ans.push_back(newInterval);
return ans;
}
if (newInterval.end < intervals[cur].start) {
st_pos = newInterval.start;
end_pos = newInterval.end;
} else {
st_pos = min(newInterval.start, intervals[cur].start);
end_pos = max(newInterval.end, intervals[cur++].end);
}
while (cur < intervals.size() && intervals[cur].start <= end_pos) {
end_pos = max(end_pos, intervals[cur++].end);
}
ans.push_back(Interval(st_pos, end_pos));
while (cur < intervals.size()) {
ans.push_back(intervals[cur++]);
}
return ans;
}
};