Merge Intervals

Desicription

Given a collection of intervals, merge all overlapping intervals.

For example,

Given [1,3],[2,6],[8,10],[15,18],

return [1,6],[8,10],[15,18].

Solution

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
/**
* Definition for an interval.
* 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;
if(intervals.empty())
return res;
sort(intervals.begin(), intervals.end(), [](Interval a, Interval b){return a.start < b.start;});
res.push_back(intervals[0]);
for(int i = 1; i < intervals.size(); i++){
if(res.back().end < intervals[i].start)
res.push_back(intervals[i]);
else
res.back().end = max(res.back().end, intervals[i].end);
}
return res;
}
};