forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge2.cpp
More file actions
43 lines (39 loc) · 1.07 KB
/
merge2.cpp
File metadata and controls
43 lines (39 loc) · 1.07 KB
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
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> res;
vector<pair<int, int> > mm;
int n = intervals.size();
if (n == 0) return res;
REP(i,n) mm.push_back(make_pair(intervals[i].start, intervals[i].end));
sort(mm.begin(), mm.end());
int lt = mm[0].first, rt = mm[0].second;
FOR(i,1,n-1) {
if (mm[i].first <= rt) rt = max(rt, mm[i].second);
else {
res.push_back(Interval(lt, rt));
lt = mm[i].first;
rt = mm[i].second;
}
}
res.push_back(Interval(lt, rt));
return res;
}
};
int main() {
return 0;
}