-
Notifications
You must be signed in to change notification settings - Fork 0
/
0056.merge_intervals.cpp
64 lines (54 loc) · 1.5 KB
/
0056.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <algorithm>
#include <iostream>
#include <vector>
using std::vector;
// [1,3] [2,6] [8,10]
vector<vector<int>> merge(vector<vector<int>>& intervals)
{
int n = intervals.size();
std::sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) -> bool {
return a[0] < b[0];
});
vector<vector<int>> res;
for (int i = 0, j = 0; i < n; i = j) {
int l = intervals[i][0];
int r = intervals[i][1];
if (i == n - 1) {
res.push_back({l, r});
}
for (j = i + 1; j < n; ++j) {
int u = intervals[j][0];
int v = intervals[j][1];
if (l <= u && u <= r) {
l = std::min(l, u);
r = std::max(r, v);
if (j == n - 1) {
res.push_back({l, r});
}
} else {
res.push_back({l, r});
break;
}
}
}
return res;
}
int main () {
#ifdef LOCAL
freopen("0056.in", "r", stdin);
#endif
int n = 0;
while (std::cin >> n) {
vector<vector<int>> intervals(n, vector<int>(2, 0));
for (int i = 0; i < n; ++i) {
std::cin >> intervals[i][0] >> intervals[i][1];
}
vector<vector<int>> res = merge(intervals);
std::cout << "[";
for (const auto es : res) {
std::cout << "[" << es[0] << "," << es[1] << "] ";
}
std::cout << "]" << std::endl;
}
return 0;
}