[牛客网-Leetcode] #Array is harder insert-interval

Insert -interval

Title description

Given a set of non-overlapping time intervals, insert a new time interval in the time interval (if there is overlap, merge the intervals).
These time intervals are initially sorted according to their start time.
Example 1:
Given a time interval [1,3], [6,9], insert a time interval [2,5] in these two time intervals, and merge it with the original time interval to become [1 ,5],[6,9].
Example 2:
Given time interval [1,2],[3,5],[6,7],[8,10],[12,16], at these times Insert the time interval [4,9] into the interval and merge it with the original time interval to become [1,2],[3,10],[12,16].
This is because the time interval [4, 9] covers the time interval [3,5],[6,7],[8,10].

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].

Example 2:
Given[1,2],[3,5],[6,7],[8,10],[12,16], insert and merge[4,9]in as[1,2],[3,10],[12,16].

This is because the new interval[4,9]overlaps with[3,5],[6,7],[8,10].

Problem-solving ideas

  • Drawing lessons from the idea of merging intervals , since the intervals are initially ordered, the intervals array is traversed to find the correct insertion position of newInterval. After inserting, it becomes a problem of merging interval, just apply the template directly.

  • Usage of insert() in vector:

    insert(it, x) is used to insert an element x to any iterator it of the vector, and the time complexity is O(N).

    vector<int> vi;
    for (int i = 1; i <= 5; i++) {
          
          
    	vi.push_back(i); //此时为1 2 3 4 5
    }
    vi.insert(vi.begin() + i, -1); //将-1插入vi中数组下标为2的位置
    /* 输出结果为1 2 -1 3 4 5 */
    

    Therefore, inserting the element x into the array at the position where the index is i is written as:
    it.insert(it.begin() + i, -1)

  • The code for this question is as follows:

/**
 * 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:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
    
    
        vector<Interval> res;
        if(!intervals.size()) {
    
    
            res.push_back(newInterval);
            return res;
        }
        //由于intervals初始有序,因此遍历数组,寻找newInterval正确的插入位置
        for(int i = 0; i < intervals.size(); i ++) {
    
    
            if(newInterval.start <= intervals[i].start) {
    
    
                intervals.insert(intervals.begin() + i, newInterval);
                break;
            }
        }
        //如果newInterval比所有区间都大,则直接添加在最后
        if(newInterval.start > intervals.back().start) {
    
    
            intervals.push_back(newInterval);
        }
        //以下为区间合并的模板
        res.push_back(intervals[0]);
        for(int i = 1; i < intervals.size(); i ++) {
    
    
            Interval temp = intervals[i];
            //如果存在重叠
            if(res.back().end >= temp.start) {
    
    
                res.back().end = max(res.back().end, temp.end);
            } else {
    
    
                res.push_back(temp);
            }
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/cys975900334/article/details/106612231