leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0056.cpp (663B)


0 class Solution {
1 typedef vector<int> interval;
3 public:
4 vector<interval> merge(vector<interval> &intervals) {
5 auto cmp = [](const interval &i1, const interval &i2) { return i1[0] < i2[0]; };
6 sort(intervals.begin(), intervals.end(), cmp);
8 vector<interval> res;
9 int start = intervals[0][0], end = intervals[0][1];
10 for (auto &i : intervals) {
11 if (i[0] > end) {
12 res.push_back({start, end});
13 start = i[0];
14 }
15 end = max(end, i[1]);
16 }
18 if (res.empty() || res.back()[1] != end) res.push_back({start, end});
20 return res;
21 }
22 };