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. php json_encode 斜杠 反斜杠 转义处理

    $data = str_replace("\\\\n", "\\n", \jsonEncode($data)); // \\n转为\n $data = str_ ...

  2. regex cheat sheet

    regex pattern visualizer : regex101: build, test, and debug regex https://regex101.com/ regex regex ...

  3. Salesforce Connect 连接两个不同的Org(实际设置方法)

    利用Salesforce的标准功能:Salesforce Connect,可以轻松的将两个组织(Org)连接起来.实现Object的共享(包括参照和编辑). 要求: ①两个组织必须是开发者Edtion ...

  4. Arch安装记录(BIOS+GPT)

    尝试了下arch安装,并且尝试了下不长用的BIOS + GPT组合.都说arch的wiki强,确实很强,可惜自己的水平看不了多少. https://wiki.archlinux.org/index.p ...

  5. Cesium开发三维地图入门

    需求:要求将GLTF三维模型放到地图上展示,并且添加各种图标和线进行标注. 用CesiumJS地图库实现代码如下: 引入CesiumJS库 1.直接clone源码包,在index.html中引入,如下 ...

  6. Fiddle 简单用法

    下载安装后,还要下载证书放到浏览器 https://zhuanlan.zhihu.com/p/439203346

  7. (K8s学习笔记三)创建Namespace

    Namespace(命名空间)很多情况下用于实现多租户的资源隔离.Namespace通过将集群内部的资源对象"分配"到不同的Namespace中,形成逻辑上分为不同项目.小组或用户 ...

  8. managing projects with GNU make pdf

    读 c++编程思想的时候作者推荐的关于makefile的书,大家随意抱走. 链接:https://pan.baidu.com/s/1k0qg9iA3V25C2yJnOi9WfQ 提取码:5vx1

  9. 离线谷歌地图API的开发笔记(二)

    一.地图引擎介绍 离线地图引擎运行在WINDOWS平台上,底层由Visual c++语言开发,编译为OCX插件方式.占用文件少,便于二次开发的快速安装部署. 具有专业地图的基础操作功能:地图放大.缩小 ...

  10. C# Linq将DataTable中的某列转换成数组或者List

    // 获取到的数据 DataTable picDt = GetPdmPoroductModelPictureData(productModelCode); // 将productCode列转数组 st ...