【LeetCode】57. Insert Interval [Interval 系列]

LeetCode中,有很多关于一组interval的问题。大体可分为两类:

1.查看是否有区间重叠; 2.合并重叠区间;  3.插入新的区间; 4. 基于interval的其他问题

【 做题通用的关键步骤】:

1. 按照begin时间排序;

2. 判断两个相邻区间是否重叠:

【假设】

a. 给定的区间是半开区间 [begin, end);

b. 已经按照"begin"排序.

c. 当前的区间是begin, end; 要比较/合并的区间:[ intervals[i][0], intervals[i][1]); (begin <= intervals[i][0])

【重合情况】

(begin == intervals[i][0] || end >= intervals[i][0])    =>          end = max(end, intervals[i][1])

在做第二类问题时,除了判断是否重叠,还需要合并重叠区间。 只要定义一个[begin, end]代表之前一个区间, 若与当前区间重叠,则更新end

 

【例题1】 第一类问题示例,Eg. 252. Meeting Rooms (比较简单代码省略)

【例题2】 第二类问题示例, Eg.56. Merge Intervals

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if(intervals.size() == 0) return intervals;
         vector<vector<int>> res;
         sort(intervals.begin(), intervals.end(), [](auto &in1, auto &in2){
             return (in1[0] < in2[0] || (in1[0] == in2[0] && in1[1] > in2[1]));
         });
         auto curr = intervals[0];
         int i = 1;
         while(i < intervals.size()) {
             if(curr[1] < intervals[i][0]) {
                 res.push_back(curr);  //无重叠,记录之前的interval; 
                 curr = intervals[i];
             }
            curr[1] = max(curr[1], intervals[i][1]); // 有重叠更新end
            i++;
         }
         res.push_back(curr);
         return res;
    }
};

时间复杂度:排序 O(nlogn); 合并 O(n)

------------------------------------------------------------------------------------------------------------------

 

【例题3】 另一道第二类问题,与string相结合的题目:616. Add Bold Tag in String

Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.

 

其实本质上也是要合并所有在dict里存在并且重叠的子字符串。基本思路是以s中每个字符为起始,找出长度最长的与dict中对应的字符串,如果有这样的字符串,就查看与之前[begin, end]是否重叠。并更新end,或将之前interval输入res中。

string addBoldTag(string s, vector<string>& dict) {
        string res = "";
        if(s.size() == 0) return res;
        unordered_map<char, vector<string>> map;
        for(auto d: dict){
            map[d[0]].push_back(d);
        }
        int begin = 0, end = -1;
        for(int i = 0; i < s.size(); ++i){
            for(auto str: map[s[i]]){
                if(i+str.size()-1 < s.size() && i+(int)str.size()-1 > end
                 && s.substr(i, str.size()) == str){
                    end = i+str.size()-1;
                }
            }
            if(i > end){
                if(begin <= end){
                    string temp = "<b>" + s.substr(begin, end-begin+1) + "</b>";
                    res += temp;
                }
                res.push_back(s[i]);
                begin = i+1;
            } 
        }
        if(begin <= end){
            string temp = "<b>" + s.substr(begin, end-begin+1) + "</b>";
            res += temp;
        }
        return res;
    }

一些减少时间复杂度的方法,是用一个hash map,根据dict中每一个word首字母为Key, 记录所有以该字母开始的word。在遍历s时,只需查看所有以s[i]起始的字符串即可;另外在确认是否匹配前,可以先查看如果匹配的话,是否会大于当前end (i+word.size()-1 > end)。大于的情况下再去比较两个字符串,以节省时间开销。

--------------------------------------------------------------------------------------------------------------------

【例题4】

第三类题目,注意包含一些边界值需要考虑:57. Insert Interval

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.

 

这道题目可以用类似的思路,即先排序后遍历,然后对所有overlap的区间进行整合,并插入到合适的位置。因为存在很多情况,所以比较容易出错。

另一种相对不易出错并且时间复杂度少一些的做法:

  通过二分查找来找到最后一个比要插入区间小的区间 (intervals[i][1] < begin ), 

  以及第一个比插入区间大的区间 (intervals[i][0]  > end )

      然后再对中间部分进行处理

class Solution {
private:
    int lastSmall(vector<vector<int>>& intervals, int begin){
        int b = 0, e = intervals.size()-1;
        while(b < e){
            if(e - b == 1){
                if(intervals[e][1] < begin) b = e;
                return b;
            }
            int mid = b + (e-b)/2;
            if(intervals[mid][1] < begin) b = mid;
            else e = mid-1;
        }
        return b;
    }
    int firstBig(vector<vector<int>>& intervals, int end){
        int b = 0, e = intervals.size()-1;
        while(b < e){
            if(e - b == 1){
                if(intervals[b][0] <= end) b = e;
                return b;
            }
            int mid = b + (e-b)/2;
            if(intervals[mid][0] > end) e = mid;
            else b = mid+1;
        }
        return b;
    }
public:
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
        vector<vector<int>> res;
        sort(intervals.begin(), intervals.end());
        int n = (int)intervals.size();
        int begin = newInterval[0], end = newInterval[1];
        int p1 = -1, p2 = n;  
        //p1是最后一个比目标区间小的区间;
        //p2是第一个比目标区间搭的区间;      
       //初始值用来标记不存在符合条件的区间
        if(n > 0 && intervals[0][1] < begin)
            p1 = lastSmall(intervals, begin);
        if(n > 0 && intervals[n-1][0] > end)
            p2 = firstBig(intervals, end);
        if(p1 >= 0) res.assign(intervals.begin(), intervals.begin()+p1+1);
        if(p1+1 < n) begin = min(intervals[p1+1][0], begin);
        if(p2-1 >= 0) end = max(intervals[p2-1][1], end);
        res.push_back(vector<int>{begin, end});
        if(p2 < n) res.insert(res.end(), intervals.begin()+p2, intervals.end());
       
        return res;
    }
};                

 

 时间复杂度: 排序 O(nlogn); 寻找目标区间 O(logn)

--------------------------------------------------------------------------------------------------------------------

下面是两道是在区间合并基础上延伸一些的题目 

【示例5】 253. Meeting Rooms II

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

这道题目其实和上边的区间合并思路不太一样。也是要先排序,然后重点是要想清楚在新安排一个会议时,把他安排在之前最早结束的没有重叠的会议室是最优选择。(因为排在他之后的会议都比他开始时间晚,会造成时间的浪费)。

基于此就要知道在安排每个会议时,之前每个会议室结束会议的时间,并且是有序的方便可以更新的。便可想到使用priority_queue

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        int n = static_cast<int>(intervals.size());
        sort(intervals.begin(), intervals.end(), [](vector<int>& i1, vector<int>& i2){return i1[0] < i2[0];});
        priority_queue<int, vector<int>, greater<int>> pq;
        //int num = 0;
        for(auto i: intervals) {
            int begin = i[0];
            int end = i[1];
            if(pq.empty() || pq.top() > begin) pq.push(end); 
            else {
                pq.pop();
                pq.push(end);
            }
        }
        return pq.size();
    }
};

 

------------------------------------------------------------------------------------------------------------------

【示例6】 715. Range Module

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

addRange(int left, int right) Adds the half-open interval [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right) that are not already tracked.
queryRange(int left, int right) Returns true if and only if every real number in the interval [left, right) is currently being tracked.
removeRange(int left, int right) Stops tracking every real number currently being tracked in the interval [left, right).

这道题目在insert interval的基础上需要动态的去维护这样一组Intervals。如果还用之前的办法,用一个数组/vector去维护,大量的插入和删除操作会导致很大的时间复杂度。此外要维持有序和查找也要花很多时间。所以用有序map来实现是一个很好的选择。

class RangeModule {
    map<int, int> maps;  //key: right/end; value: left/begin
public:
    RangeModule() {

    }
    
    void addRange(int left, int right) {
        auto it = maps.lower_bound(left);
        if(it != maps.end()) left = min(left, it->second);
        while(it != maps.end() && it->second <= right){
            right = max(right, it->first);
            auto temp = it;
            it++;
            maps.erase(temp);
        }
        maps.insert(make_pair(right, left));
    }
    
    bool queryRange(int left, int right) {
        auto it = maps.upper_bound(left);
        return (it != maps.end() && it->first >= right && it->second <= left);
    }
    
    void removeRange(int left, int right) {
        auto it = maps.upper_bound(left);
        int l1 = -1, r2 = -1;
        while(it != maps.end() && it->second < right) {
            if(it->second < left) {
                l1 = it->second;
            }
            if(it->first > right) {
                r2 = it->first;
            }
            auto temp = it;
            it++;
            maps.erase(temp);
        }
        //cout << l1 << ", " << r2 << endl;
        if(l1 != -1) maps.insert(make_pair(left, l1));
        if(r2 != -1) maps.insert(make_pair(r2, right));
    }
};

/**
 * Your RangeModule object will be instantiated and called as such:
 * RangeModule* obj = new RangeModule();
 * obj->addRange(left,right);
 * bool param_2 = obj->queryRange(left,right);
 * obj->removeRange(left,right);
 */

时间复杂度: O(nlogn); 空间额外开销: O(n)   //map 

---------------------------------------------------------------------

以上题目均来源:

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/meeting-rooms-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

--------------------------------------------------------------------- 

上一篇:python多进程之multiprocessing


下一篇:Kafka核心技术与实战——19 | CommitFailedException异常怎么处理?