-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert Interval.cpp
More file actions
36 lines (35 loc) · 1.36 KB
/
Insert Interval.cpp
File metadata and controls
36 lines (35 loc) · 1.36 KB
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
// Two overlapping intervals itv1 and itv2, the merge interval itv;
// itv.start = min (itv1.start, itv2.start); itv.end = max(itv1.end, itv2.end);
// space complexity: O(1)
// time complexity: O(n)
/**
* 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) {
vector<Interval> res;
int i = 0;
// push the intervals on the left of the newInterval into the result vector;
while (i < intervals.size() && intervals[i].end < newInterval.start)
res.push_back(intervals[i++]);
// if there is an interval overlapping with the newInterval, update the newInterval
while (i < intervals.size() && newInterval.end >= intervals[i].start) {
newInterval.start = min(intervals[i].start, newInterval.start);
newInterval.end = max(intervals[i].end, newInterval.end);
i++;
}
// push the updated newInterval into the result vector;
res.push_back(newInterval);
// push the intervals on the right of the newInterval into the result vector;
while (i < intervals.size())
res.push_back(intervals[i++]);
return res;
}
};