57. Insert Interval

  1. 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.

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
在这里插入图片描述

class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        i = len(intervals)-1
        k = i+1
        while i >= 0:
            if newInterval[0] <= intervals[i][0]:
                k = i
            i -= 1
        intervals.insert(k, newInterval)

        i = 0
        while (i+1) < len(intervals):
            A = intervals[i]
            B = intervals[i+1]

            if (max(A[1],A[0],B[1],B[0]) - min(A[1],A[0],B[1],B[0])) <= (A[1]-A[0]+B[1]-B[0]):
                intervals.remove(A)
                B[0], B[1] = min(A[1],A[0],B[1],B[0]), max(A[1],A[0],B[1],B[0])
                continue

            i += 1

        return intervals
  • 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、付费专栏及课程。

余额充值