-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP56.cpp
76 lines (69 loc) · 1.65 KB
/
P56.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
65
66
67
68
69
70
71
72
73
74
75
76
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
#include<stack>
using namespace std;
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
bool cmpS(const Interval &a, const Interval &b) {
return a.start<b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
if (intervals.empty() || intervals.size()<=1) return intervals;
sort(intervals.begin(),intervals.end(),cmpS);
vector<Interval> ans;
auto i=intervals.begin();
int s,e;
s=i->start;e=i->end;
i++;
while (i!=intervals.end()) {
if (e>=i->start) {
e=max(e,i->end);
}
else {
ans.push_back(Interval(s,e));
s=i->start;
e=i->end;
}
i++;
}
ans.push_back(Interval(s,e));
return ans;
}
};
static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}();
int main() {
vector<Interval> a;
a.push_back(Interval(1,4));
a.push_back(Interval(4,5));
a.push_back(Interval(5,6));
a.push_back(Interval(2,3));
a.push_back(Interval(7,8));
auto res = Solution().merge(a);
for (auto v:res)
cout << v.start << " " << v.end<< endl;
return 0;
}