-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0057-insert-interval.cpp
More file actions
29 lines (28 loc) · 945 Bytes
/
0057-insert-interval.cpp
File metadata and controls
29 lines (28 loc) · 945 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
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> ans;
bool inserted = false;
for (int i = 0; i < intervals.size(); ++i) {
vector<int>& cur = intervals[i];
if (inserted) {
ans.push_back(cur);
continue;
}
if (newInterval[0] > cur[1]) {
ans.push_back(cur);
} else {
if(cur[0] > newInterval[1]) {
inserted = true;
ans.push_back(newInterval);
ans.push_back(cur);
} else {
newInterval[1] = max(newInterval[1], cur[1]);
newInterval[0] = min(newInterval[0], cur[0]);
}
}
}
if (!inserted) ans.push_back(newInterval);
return ans;
}
};