-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
41 lines (39 loc) · 965 Bytes
/
main.cpp
File metadata and controls
41 lines (39 loc) · 965 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
36
37
38
39
40
41
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> res;
if(intervals.size() < 1) return res;
sort(intervals.begin(), intervals.end());
res.push_back(intervals[0]);
for(int i = 1; i < intervals.size(); ++i)
{
if(res.back().back() < intervals[i].front())
{
res.push_back(intervals[i]);
}
else{
res.back().back() = max(res.back().back(), intervals[i].back());
}
}
return res;
}
};
int main()
{
Solution s;
vector<vector<int>> intervals = {{1,3},{2,6},{8,10},{15,18}};
vector<vector<int>> res;
res = s.merge(intervals);
for(auto & tmp : res)
{
for(auto & i : tmp)
{
cout << i << endl;
}
}
return 0;
}