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 ...
随机推荐
- 并发多线程学习(三)Java多线程入门类和接口
1 Thread类和Runnable接口 上一章我们了解了操作系统中多线程的基本概念.那么在Java中,我们是如何使用多线程的呢? 首先,我们需要有一个"线程"类.JDK提供了Th ...
- 批量检测URL中的关键词,并且将不含有关键词的URL输出到txt文件当中
编写代码时遇到的问题 写入txt(一开始忘记了tuple类型需要转换为str) 处理逻辑(一开始并没有将 body与url绑定到一起,所以或返回所有的url) 关闭太早(这点是有点疑惑的,难道不用关闭 ...
- SpringBoot启动流程与自动装配
是什么 Spring Boot基于Spring框架之上的一个微服务架构开发框架大大简化了Spring的开发.因为Spring Boot提供了大量的自动配置.而且它是基于Java配置方式的开发(全注解) ...
- spark闭包检查
spark在执行算子时,如果算子内部用到了外部(Driver)端的对象或变量,就一定会出现闭包:spark在执行算子之前会进行闭包检查,也就是对外部对象或变量进行序列化检查:
- wpf 自定义Messagebox时,对话框显示不居中问题
在自定义Messagebox(有属性Window.SizeToContent="WidthAndHeight")时,对话框显示不居中,经过尝试,应设置如下: msgBox.Wind ...
- WPF中获取主窗口 MainWindow 实例,以及在其他窗口中获取 MainWinodw 中的控件
var _mainWindow = Application.Current.Windows .Cast<Window>() .FirstOrDefault(window => win ...
- fetchAll 的小小分析
includes\database\prefetch.inc line 425 $this->defaultFetchStyle: fetch_object int 5protected $de ...
- 解决QtCreator运行程序报plugin xcb的错误
解决方法:将对应项目的运行环境的LD_LIBRARY_PATH中的qt的库路径移到最前面,如下图: LD_LIBRARY_PATH可以指定查找共享库的路径,将qt的共享库移到前面,可以优先使用qt的库
- 第6课第4节_Binder系统_驱动情景分析_服务注册过程_分析
Service_manager.c 8074 2017/6/30 首先从应用程序分析(Android) binder_loop(bs, svcmgr_handler) { readbuf[0] = B ...
- vulnhub:easy_cloudantivirus靶机
kali:192.168.111.111 靶机:192.168.111.177 信息收集 端口扫描 nmap -A -v -sV -T5 -p- --script=http-enum 192.168. ...