Skip to content

Latest commit

 

History

History

insert-interval

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Insert interval

Problem link

Solutions

Solution.cpp

// https://leetcode.com/problems/insert-interval

class Solution {
 public:
  vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& n) {
    vector<vector<int>> ret;
    bool flag = true;
    for (auto& p : intervals) {
      if (p[1] < n[0])
        ret.push_back(p);
      else if (p[0] > n[1]) {
        if (flag) {
          ret.push_back(n);
          flag = false;
        }
        ret.push_back(p);
      } else {
        n[0] = min(p[0], n[0]);
        n[1] = max(p[1], n[1]);
      }
    }
    if (flag) ret.push_back(n);
    return ret;
  }
};

Tags