refer to: https://www.algoexpert.io/questions/Merge%20Overlapping%20Intervals


Problem Statement

Sample example

Analysis

step 1: sort the intervals by their start value

step 2: check if the end value of the previous interval is larger than the start value of the latter interval

Code

def mergeOverlappingIntervals(intervals):
# sort the intervals by startinf value. O(nlogn)
sortedIntervals = sorted(intervals, key = lambda x: x[0]) mergedIntervals = []# space: O(n)
currentInterval = sortedIntervals[0]#initialize the currentInterval as the first interval of the intervals
mergedIntervals.append(currentInterval)#initialize the mergedIntervals as the first interval of the intervals for nextInterval in sortedIntervals:# for the first iteration, no update
_, currentIntervalEnd = currentInterval
nextIntervalStart, nextIntervalEnd = nextInterval if currentIntervalEnd >= nextIntervalStart: # overlap found, update the end value of interval
currentInterval[1] = max(currentIntervalEnd, nextIntervalEnd)
else: # no overlap, add the current(nextInterval) into the mergedIntervals array
currentInterval = nextInterval
mergedIntervals.append(currentInterval)
return mergedIntervals

Time and Space complexity

O(nlogn) time complexity for sorting

O(n) space complexity for store the mergedIntervals(upper bound, all intervals kept)

Merge Overlapping Intervals的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  2. Leetcode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  4. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  5. leetcode56. Merge Intervals

    题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...

  6. Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  8. 【Leetcode】【Hard】Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  9. Java for LeetCode 056 Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  10. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. 读后笔记 -- Python 全栈测试开发 Chapter11:Python + Requests 实现接口测试

    11.1 Requests 框架 11.1.1 requests 请求 1. reqeusts 库 安装 pip install requests 2. requests 库 GET 方法,参数通过 ...

  2. jar包启动脚本, 以及外置配置文件application.yml

    想使用sh脚本来启动,停止,重启我们的jar服务, 顺便还要外置配置文件方便修改 示例server.sh如下,启动命令为sh server.sh start或restart或stop,修改其中的这几个 ...

  3. 为什么用postman

    1,  保存测试的记录, 就是确保访问的地址,数据什么的,得到的结果是有效的2,  假设开始一个访问, 然后方便在本地debug, 因为有些访问是不能通过本地的浏览器来访问的, 比如前后端分离的情况, ...

  4. mysql索引 数据库优化

    1.mysql索引结构b+树 a.首先要说二叉树,二叉查找树,数的结构不用多说,二叉查找树,大概就是几个原则,左边比右边的小,然后保持一个分布均匀,也就是树的高度尽量最小. b.b-树,b-树和二叉查 ...

  5. MYSQL5.7索引异常引发的锁超时处理记录

    原始sql: update a set a.x=x where a.xid in (select b.xid from b inner join c on b.xxx = c.xxx) and a.x ...

  6. git常用命令与AndroidStudio常用快捷键

    git相关内容: 产生密钥:cd ~/.ssh (C:\Users\账户名称\.ssh)生成密钥:ssh-keygen -t rsa -C "your_email@youremail.com ...

  7. Day 22 22.1.2:增量式爬虫 - 场景2的实现

    场景2的实现: 数据指纹 使用详情页的url充当数据指纹即可. 创建爬虫爬虫文件: cd project_name(进入项目目录) scrapy genspider 爬虫文件的名称(自定义一个名字即可 ...

  8. 基础篇之DOS命令

    对于编程的小朋友来说,在DOS中输入一些命令也是常有的事情. 今天就来学习一些常见的命令.[该篇是在B站学习Siki学院的课程时做的笔记] DOS命令目录操作 常用DOS命令(输入命令后按下回车) d ...

  9. (面试加分新技能) 总结11个ES2022中你可能遗漏的语法

    与许多其他编程语言一样,JavaScript 也在不断发展,每年,该语言都会通过新功能变得更强大,让开发人员编写更具表现力和简洁的代码.让我们探索 ECMAScript 2022 (ES13) 中添加 ...

  10. oracle 函数instr