-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge-Intervals.cpp
40 lines (36 loc) · 1019 Bytes
/
Merge-Intervals.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
39
40
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> merge(vector<vector<int>> &intervals)
{
vector<vector<int>> res;
int n = intervals.size();
sort(intervals.begin(), intervals.end(), [](const vector<int> &a, const vector<int> &b) {
if (a[0] == b[0])
return a[1] < b[1];
return a[0] < b[0];
});
int m, M, i = 0;
while (i < n)
{
m = intervals[i][0];
M = intervals[i][1];
int j = i + 1;
for (; j < n; j++)
{
if (m <= intervals[j][0] && intervals[j][0] <= M || m <= intervals[j][1] && intervals[j][1] <= M)
{
m = min(m, intervals[j][0]);
M = max(M, intervals[j][1]);
}
else
break;
};
res.push_back({m, M});
i = j;
};
return res;
}
};