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 ...
随机推荐
- JQuery的dataTable实现分页
关于dataTable基本使用有很多帖子说的很详细,在此不做详述. 最近使用dataTable处理服务器返回分页数据时遇到问题,问题解决后有一些心得分享一下: 1. 如果打开界面通过dataTable ...
- ideavimrc 示例
我自己的idea vim配置,用熟悉了以后还真的挺方便的 比较常用的有 ManageRecentProjects,快速切换多个project,经常会遇到同时打开多个project,来回切换方便多了 H ...
- 像MIUI一样做Zabbix二次开发(4)——乐维监控定制Zabbix的方法
做监控源于我们早期做运维服务的必然需求,我们的愿望是基于Zabbix这个强大开源平台,结合实际一线运维工作的需要+ITIL等运维理论,做成类似MIUI一样的开发.易用.实用.人性和美观的全新的监控平台 ...
- Dilated Neighborhood Attention Transformer概述
0.前言 相关资料: arxiv github 论文解读 论文基本信息: 发表时间:arxiv2022(2022.9.29) 1.针对的问题 之前的方法通过局部注意力机制来降低计算复杂度,但这削弱了自 ...
- 1792. 最大平均通过率 (Medium)
问题描述 1792. 最大平均通过率 (Medium) 一所学校里有一些班级,每个班级里有一些学生,现在每个班都会进行一场期末考试.给你一个二维数组 classes ,其中 classes[i] = ...
- 项目_1.3_手写SpringMVC框架
初步修改项目 package com.hy.servlet; import java.io.IOException; import java.lang.reflect.InvocationTarget ...
- 【C学习笔记】day2-4 将三个数按从大到小输出
#include <stdio.h> int main() { int max, min, mid; int a = 324, b = 435, c = 43; if (a > b) ...
- FTP替代传输方案的优异性体现在哪些方面?
多年来,FTP一直是最常见的交换文件的方式,FTP-FTPS-SFTP似乎是FTP的不断迭代更新,但是究竟是技术更新导致FTP过时?还是它真的已经满足不了企业的需求了? 之前,大家选择FTP往往是因为 ...
- jQuery 使用手册(一)
一:核心部分$(expr)说明:该函数可以通过css选择器,Xpath或html代码来匹配目标元素,所有的jQuery操作都以此为基础参数:expr:字符串,一个查询表达式或一段html字符串例子:未 ...
- css实现文字多余显示省略号
只显示一行文字 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 显示多行文字 word-break:break-all; ...