57. Insert Interval

此题有两种思路。一种思路为:先按照start值在原数组中二分查找待插入的区间,假设查找到的位置为ite,从ite或者ite-1开始合并区间直到不能合并为止(终止条件是合并后区间的end<当前区间的start),然后在原数组中删除参与合并的区间,再插入合并后的新区间。

在原始数组上进行操作的代码如下:

class Solution {
private:
    static bool comp(Interval a, Interval b)
    {
        return a.start < b.start;
    }
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        //在原始数组上进行操作
        vector<Interval>::iterator ite = lower_bound(intervals.begin(),intervals.end(), newInterval, comp);//按照start值二分查找
        if(ite != intervals.begin() && newInterval.start <= (ite-1)->end)//ite的上一个区间也可能参与合并
        {
            ite--;
            //ite->start一定小于等于newInterval.start,因为数组按区间起点有序
            newInterval.start = ite->start;
        }
        vector<Interval>::iterator eraseBegin = ite;
        for(; ite != intervals.end() && newInterval.end >= ite->start; ite++)
            if(newInterval.end < ite->end)newInterval.end = ite->end;//合并后的新区间存放于newInterval
         
        ite = intervals.erase(eraseBegin, ite);//[eraseBegin, ite)是合并时应该删掉的区间
        intervals.insert(ite, newInterval);//插入合并后的区间
        return intervals;
    }
};

在新数组上保存结果的代码如下:

class Solution {
private:
    static bool comp(Interval a, Interval b)
    {
        return a.start < b.start;
    }
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> res;
        res.reserve(intervals.size());
        int i;
        //插入前部分不需要合并的区间
        for(i = 0; i < intervals.size() && intervals[i].end < newInterval.start; i++)
            res.push_back(intervals[i]);
        //i为需要合并的起点,注意的是合并后新区间的起点只和第一个合并的区间有关,因为数是时按区间起点有序的
        if(i < intervals.size())newInterval.start = min(newInterval.start, intervals[i].start);
         
        //合并区间
        for(; i < intervals.size() && newInterval.end >= intervals[i].start; i++)
            if(newInterval.end < intervals[i].end)newInterval.end = intervals[i].end;
        //插入合并后的区间
        res.push_back(newInterval);
        //插入剩余的区间
        res.insert(res.end(), intervals.begin()+i, intervals.end());
        return res;
    }
};
另外一种思路是遍历数组, 如果当前interval在newInterval后面,则将当前interval插入结果中;如果当前interval在newInterval前面,则将newInterval插入结果并把newInterval赋值为当前interval;如果两者重叠,则合并两个区间,修改newInterval的数值,这三种情形如下图所示。图中的Current对应 newInterval,New对应 intervals[i]。


代码如下所示:

class Solution {
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> res;
        
        for(int i=0; i<(int)intervals.size(); i++)
            if(intervals[i].end < newInterval.start){
                res.push_back(intervals[i]);
            }else if(intervals[i].start > newInterval.end){
                res.push_back(newInterval);
                newInterval = intervals[i];
            }else {
                newInterval.start = min(newInterval.start, intervals[i].start);
                newInterval.end = max(newInterval.end, intervals[i].end);

            }
        res.push_back(newInterval);
        return res;
    }
};

此题需要注意的一点trick是当需要合并区间时,我们动态改变 newInterval的起点和结尾,这样会让逻辑更加清晰。另外以前换组的时候,面过这道题,但是出题的人,添加了一个附属条件,这些interval是在一个圆环上。比如当环是[1, 255]时,如果两个Interval分别是[1,234], [222, 4], Merge最后的结果是[1,4]。这个条件完全是个干扰,因为如果真的在逻辑中考虑环的话,非常麻烦,而且在环上做循环合并的话,无法判断何时该终止循环。 当时我的解法就是,如果第一个是跨越0点的话(比如[234,7]),把它拆分成两个interval([234, 255],[1,7]), 然后把[1,7]插到队头,[234,255]插到队尾。 从[1,7]开始往后合并,合并完成后,再检查对头及队尾,需要的话,再合并一次即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[2\]中提到的错误是由于使用PyInstaller打包时出现的问题。这些错误可能是由于PyInstaller无法正确处理某些特定的Python模块或库导致的。解决这些错误的方法可能是更新PyInstaller版本,或者尝试使用其他打包工具。 至于引用\[3\]中提到的错误,即"'pandas._libs.interval.Interval' object has no attribute 'total_iv'",这个错误是由于在代码中使用了pandas._libs.interval.Interval对象的total_iv属性,但该属性在该对象中并不存在。这可能是由于使用了错误的属性名称或者版本不兼容导致的。要解决这个错误,您可以检查代码中对该属性的使用,并确保使用正确的属性名称。如果您使用的是较旧的pandas版本,可能需要升级到较新的版本以获得所需的属性。 总结起来,要解决这些错误,您可以尝试以下几个步骤: 1. 更新PyInstaller版本或尝试其他打包工具。 2. 检查代码中对属性的使用,并确保使用正确的属性名称。 3. 如果使用的是较旧的pandas版本,考虑升级到较新的版本以获得所需的属性。 希望这些解决方案能帮助您解决问题! #### 引用[.reference_title] - *1* *2* [成功解决pyinstaller打包AttributeError:type object pandas._TSObject has no attribute _reduce_cython_](https://blog.csdn.net/qq_41185868/article/details/80601983)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ModuleNotFoundError: No module named ‘pandas._libs’](https://blog.csdn.net/yangning7777777/article/details/120909561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值