forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0057-insert-interval.cpp
38 lines (30 loc) · 1.04 KB
/
0057-insert-interval.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
/*
Given array of non-overlapping intervals & a new interval, insert & merge if necessary
Ex. intervals = [[1,3],[6,9]], newInterval = [2,5] -> [[1,5],[6,9]]
To merge: while intervals are still overlapping the new one, take the larger bounds
Time: O(n)
Space: O(n)
*/
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int i = 0;
int n = intervals.size();
vector<vector<int>> result;
while (i < n && intervals[i][1] < newInterval[0]) {
result.push_back(intervals[i]);
i++;
}
while (i < n && intervals[i][0] <= newInterval[1]) {
newInterval[0] = min(newInterval[0], intervals[i][0]);
newInterval[1] = max(newInterval[1], intervals[i][1]);
i++;
}
result.push_back(newInterval);
while (i < n) {
result.push_back(intervals[i]);
i++;
}
return result;
}
};