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 ...
随机推荐
- 大小写字符转换【Sql Server和C#两种写法】
案例:Var Str = "abdCnd" 如何将Str = "ABDCND"? Sql Server写法:upper(Str) ==> Lower ...
- httpclint的传值和访问https
一.StringContent与FormUrlEncodedContent 可参考这篇文章写的非常好: https://blog.csdn.net/lxrj2008/article/details/7 ...
- PowerShell学习笔记三_使用PS操作远程服务器
PowerShell远程操作服务器 参考: https://www.cnblogs.com/sparkdev/p/7200004.html 补充: 1. 服务器上,要被Powershell远程操作,是 ...
- office图标变白新的处理方法
https://www.haozhuangji.com/xtjc/133013759.html 一般搜索得到的处理方式与上面链接的处理方式差不多,都是通过安装wps或者修改注册表来实现的. 本文是我在 ...
- 查询dockerhub中某镜像所有版本
curl https://registry.hub.docker.com/v1/repositories/${imagename}/tags | tr -d '[[]" ]' | tr '} ...
- .net ef 链接 mysql
https://blog.csdn.net/weixin_30394975/article/details/114168133
- Msp430 编写交通灯程序
题目:我想想... 红灯亮,按下按键后倒计时10秒,倒计时十秒后,绿灯点亮,红灯熄灭,进入绿灯的15秒倒计时,在只剩下3秒的时候,绿灯闪烁. 代码如下,有点麻烦 当时这么写的 就不改了 #includ ...
- PHP开启缓存加速
PHP默认会将Operate Code文件丢弃,缓存加速是将其保存下来,放置共享内存中,以便在下次调用该PHP页面时重用,避免相同代码的重复编译 __________________________ ...
- Background Suppression Network for Weakly-supervised Temporal Action Localization概述
0. 前言 相关资料: arxiv github 论文解读1,论文解读2 论文基本信息: 领域:弱监督时序行为定位 发表时间:AAAI 2020(2019.11.22) 1.针对的问题 弱监督视频动作 ...
- [node]把静态html挂到node接口下
主要适用于同网段下不同设备查看同一html. 来自知乎. 需要先安装一个node,安装过程不表. 新建文件:nodeServer.js var express = require('express') ...