-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMerge Intervals.cpp
More file actions
34 lines (33 loc) · 885 Bytes
/
Merge Intervals.cpp
File metadata and controls
34 lines (33 loc) · 885 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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(Interval a, Interval b){
return a.start<b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> res;
int n = intervals.size();
if (n == 0) return res;
sort(intervals.begin(), intervals.end(), cmp);
int l = intervals[0].start, r = intervals[0].end;
for (int i = 1; i<n; i++){
Interval &x = intervals[i];
if (r<x.start){
res.push_back(Interval(l, r));
l = x.start, r = x.end;
}else{
r = max(r, x.end);
}
}
res.push_back(Interval(l, r));
return res;
}
};