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)


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