-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56.cpp
More file actions
21 lines (21 loc) · 668 Bytes
/
56.cpp
File metadata and controls
21 lines (21 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(),
[](vector<int>& a, vector<int>& b) {
return a[0] < b[0] || (a[0] == b[0] && a[1] < b[1]);
});
vector<vector<int>> result = {intervals[0]};
int i = 0;
auto j = intervals.begin() + 1;
for (; j != intervals.end(); ++j) {
if (result[i][1] >= (*j)[0]) {
result[i][1] = max((*j)[1], result[i][1]);
} else {
++i;
result.push_back(*j);
}
}
return result;
}
};