/**
* 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:
struct intervalKey {
inline bool operator()(const Interval& in1, const Interval& in2) {
return in1.start < in2.start;
}
};
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> res;
if (intervals.size() == 0) {
return res;
}
sort(intervals.begin(), intervals.end(), intervalKey());
int cnt = 0, n = intervals.size();
res.push_back(intervals[0]);
for (int i = 1; i < n; i++) {
if (intervals[i].start <= res[cnt].end) {
res[cnt].end = max(intervals[i].end, res[cnt].end);
} else {
cnt++;
res.push_back(intervals[i]);
}
}
return res;
}
};