-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert Interval.cpp
32 lines (29 loc) · 1 KB
/
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
// Runtime: 20 ms, faster than 97.04% of C++ online submissions for Insert Interval.
// Memory Usage: 17 MB, less than 100.00% of C++ online submissions for Insert Interval.
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> ans;
int i=0;
while(i<intervals.size() && intervals[i][1] <newInterval[0])
{
ans.push_back(intervals[i]);
i++;
}
if (i < intervals.size() && intervals[i][0] < newInterval[0]) {
newInterval[0]=intervals[i][0] ;
}
while(i < intervals.size() && intervals[i][0] <= newInterval[1]) {
if (i < intervals.size() && newInterval[1] < intervals[i][1]){
newInterval[1] = intervals[i][1];
}
i++;
}
ans.push_back(newInterval);
while(i < intervals.size()) {
ans.push_back(intervals[i]);
i++;
}
return ans;
}
};