-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert-Interval.cpp
More file actions
35 lines (34 loc) · 919 Bytes
/
Insert-Interval.cpp
File metadata and controls
35 lines (34 loc) · 919 Bytes
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
class Solution
{
public:
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval)
{
vector<vector<int>> ans;
int left = newInterval[0], right = newInterval[1];
bool placed = false;
for (const vector<int> &interval : intervals)
{
if (interval[0] > right)
{
if (!placed)
{
ans.push_back({left, right});
placed = true;
};
ans.push_back(interval);
}
else if (interval[1] < left)
{
ans.push_back(interval);
}
else
{
left = min(left, interval[0]);
right = max(right, interval[1]);
};
};
if (!placed)
ans.push_back({left, right});
return ans;
}
};