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 ...
随机推荐
- ansible笔记第二章(ansible-varable变量)
(1)变量类型 1.1在playbook文件中的play使用变量 [root@m01 project1]# cat vars_1.yml - hosts: oldboy vars: - web_p ...
- ABC 171 F - Strivore 【容斥】
https://atcoder.jp/contests/abc171/tasks/abc171_f 题意 给你一个数 \(k\) ,一个字符串 \(s\) (只包含小写字母) 定义一次操作:把任意小写 ...
- Vue + Element table的@select方法获取当table中的id值都相同时,获取他们索引
先说下问题情况,原本通过双重forEach方法方法,遍历可以获取到被勾选中的索引. let arr = []val.forEach((val, index) => { this.TableDat ...
- 监听 view 初始化时
new ViewTreeObserverRegister().observe(getContentView(), new ViewTreeObserver.OnGlobalLayoutListener ...
- Linux下查找并杀死 zombile 和 stopped 进程
用top命令查看系统运行情况,突然发现stopped和zombile进程个数居然不是0. [root@myos software]# top top - 11:20:17 up 60 days, 17 ...
- vi 异常退出出现 E325:Attention的解决办法
在linux系统下使用vi编辑程序的时候,没有保存退出,直接关闭了,出现了以下的情况: 打开就会显示filename.c.swap已经存在. 这是因为vi在编辑文件时会创建一个交换文件swap fil ...
- 更换CentOS的下载源为阿里云
阿里Linux镜像地址:http://mirrors.aliyun.com/ 1.备份mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...
- SqlServer outer apply(cross apply)
select * from baiduacg_cookies c cross apply (select top 1 * from product where AccountId=c.AccountI ...
- 在centons7系统部署一套单master的k8s集群
架构图: 操作系统:CentOS Linux release 7.7.1908 (Core) docker:docker-ce-20.10.14-3.el7.x86_64 kubernetes: 1. ...
- Word06 黑客技术office真题
1.课程的讲解之前,先来对题目进行分析,首先需要在考生文件夹下,将Wrod素材.docx文件另存为Word.docx,后续操作均基于此文件,否则不得分. 2.这一步非常的简单,打开下载素材文件,在[文 ...