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. wpf DataGrid cell 背景色修改参考

    <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource ...

  2. pip安装清华源

    一.更换PIP源PIP源在国外,速度慢,可以更换为国内源,以下是国内一些常用的PIP源. 豆瓣(douban) http://pypi.douban.com/simple/ (推荐)清华大学 http ...

  3. kill 多个进程的脚本

    杀死多个进程的脚本 #!/bin/bash pids=$(ps -ef | grep warehouse |awk '{print $2}') for pid in $pids do echo $pi ...

  4. TPM 2.0 - could not load "libtss2-tcti-tabrmd.so.0"

    报错: TPM 2.0 - could not load "libtss2-tcti-tabrmd.so.0" 解决:sudo apt install libtss2-tcti-t ...

  5. 使用Swagger和OpenAPI 3规范定义API接口并集成到SpringBoot

    1. OpenAPI 3 规范介绍及属性定义 参考官方定义:https://swagger.io/specification/ 2. 使用OpenAPI 3规范定义API接口 官方样例参考:https ...

  6. 1071 - Specified key was too long; max key length is 767 bytes

    set global innodb_large_prefix=on;set global innodb_file_format=BARRACUDA; 主从都要修改以上2个参数.

  7. django验证码模块django-simple-captcha的使用介绍

    django-simple-captcha是django验证码模块,非常方便易用. 1.环境的准备: 在django项目环境中安装:pip install django-simple-captcha ...

  8. PLSQL REPLACE 函数替换操作

    oracle REPLACE 函数是用另外一个值来替代串中的某个值. 例如,可以用一个匹配数字来替代字母的每一次出现. REPLACE 的格式如下: REPLACE ( char, search_st ...

  9. JavaScript垃圾回收机制的了解

    对于js种的任意长度字符串,对象,数组是没有固定大小的,只有在分配存储时,解释器就会分配内存来存储这些数据.当js的解释器消耗完系统所有可用内存时,就会造成系统崩溃.因此js有着自己的一套垃圾回收机制 ...

  10. 宝塔面板Nginx开启gzip,提高网站访问速度的方法

    这篇文章主要为大家详细介绍了宝塔面板Nginx开启gzip,提高网站访问速度的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴. 最近有用户问小编说在宝塔面板N ...