Merge Overlapping Intervals
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的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- leetcode56. Merge Intervals
题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...
- Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【Leetcode】【Hard】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- Java for LeetCode 056 Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- TS补充笔记
TS掘金笔记:https://juejin.cn/post/6872111128135073806 *为疑惑点 类型总结: 2.6.1Enum类型数字枚举设置初始值: 2.6.1.1给第一个枚举 ...
- 批量统一调整PDF页面尺寸大小
先合并所有PDF页面到同一个文件 使用福昕PDF编辑版打开合并后的文件,选择打印功能,选择福昕虚拟打印机,打印处理器选择"比例"模式 打印并保存文件 检查新文件各页面是否大小一致并 ...
- docker 安装portainer容器后,启动/Portainer 安装MySQL并开启远程访问
启动命令: docker run -d -p 9000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --nam ...
- Kotlin源码分析 - 元编程(使用自身语言编写生成自身代码)
Kotlin源码分析 Kotlin模块FIR分析发现,在生成fir tree的时候,kotlin使用了元编程的技术,以前看到这个技术还是在JastAdd上,使用jastadd语法去写代码,生成Java ...
- HybridCLR热更新方案
Hybrid指的是混合开发,CLR指的是公共语言运行库(Common Language Runtime)->托管代码执行核心中的引擎.前身叫做huatuo git示例项目地址为https://g ...
- mybatis-plus逻辑删除deleted
项目中数据库表设计原则用到了逻辑删除:数据本身没有被删除,只是将deleted字段设置为1 mybatis-plus在逻辑删除方面的设置如下: mybatis-plus: configuration: ...
- python学习:窗口程序
https://www.cnblogs.com/zyg123/p/10385456.html # 导入tkinter模块 import tkinter # 创建画布需要的库 from matplotl ...
- mysql在windows下安装
参考博客:https://blog.csdn.net/weixin_43423484/article/details/124408565
- 如何使用cmd(dos命令)关闭IIS中某个站点
在 目录 C:\Windows\System32\inetsrv 下面有一个 appcmd 程序,定位到 该目录下 appcmd site /? #管理站点 appcmd /? #管理整个IIS ...
- Hub
public class StreamHub : Hub { public ChannelReader<string> ReadLogStream() { var channel = Ch ...