leetcode

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

0057.cpp (744B)


      1 class Solution {
      2     typedef vector<int> Interval;
      3 
      4   public:
      5     vector<Interval> insert(vector<Interval> &intervals, Interval &newInterval) {
      6         if (intervals.empty()) return {newInterval};
      7 
      8         vector<Interval> res;
      9         int n = intervals.size(), i = 0;
     10         for (; i < n && intervals[i][1] < newInterval[0]; i++) {
     11             res.push_back(intervals[i]);
     12         }
     13 
     14         for (; i < n && newInterval[1] >= intervals[i][0]; i++) {
     15             newInterval[0] = min(newInterval[0], intervals[i][0]);
     16             newInterval[1] = max(newInterval[1], intervals[i][1]);
     17         }
     18 
     19         res.push_back(newInterval);
     20 
     21         for (; i < n; i++) {
     22             res.push_back(intervals[i]);
     23         }
     24 
     25         return res;
     26     }
     27 };