本文为Python算法题集之一的代码示例
题目56:合并区间
说明:以数组 intervals
表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi]
。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。
示例 1:
输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
输出:[[1,6],[8,10],[15,18]]
解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入:intervals = [[1,4],[4,5]]
输出:[[1,5]]
解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。
提示:
-
1 <= intervals.length <= 104
-
intervals[i].length == 2
-
0 <= starti <= endi <= 104
- 问题分析
- 本题为求区间数组的合并
- 主要的计算为2个,1区间遍历,2区间合成
- 优化思路
-
减少循环层次
-
增加分支,减少计算集合
-
通过题目分析最优解
- 区间数组排序后,运算情况会大为简单
-
采用内置算法提升计算效率
CheckFuncPerf
是我写的函数用时和内存占用模块,地址在这里:Python算法题集_检测函数用时和内存占用的模块【自搓】
-
标准求解,双层循环,鼻青脸肿但通过,超过5%
import CheckFuncPerf as cfpdef merge_base(intervals):if not intervals:return []if len(intervals) == 1:return intervalsresult = []while intervals:tmpArea = intervals.pop(0)bPop = Truewhile bPop:iPopCnt = 0bPop = Falsefor iIdx in range(len(intervals)):if intervals[-iIdx-1+iPopCnt][0] > tmpArea[1]:continueif intervals[-iIdx-1+iPopCnt][1] < tmpArea[0]:continueaddArea = intervals.pop(-iIdx-1+iPopCnt)iPopCnt += 1bPop = TruetmpArea[0] = min(tmpArea[0], addArea[0])tmpArea[1] = max(tmpArea[1], addArea[1])result.append([tmpArea[0], tmpArea[1]])return resultintervals = [[1, 3], [2, 6], [8, 10], [15, 18]] result = cfp.getTimeMemoryStr(merge_base, intervals) print(result['msg'], '执行结果 = {}'.format(result['result']))# 运行结果 函数 merge_base 的运行时间为 1.00 ms;内存使用量为 4.00 KB 执行结果 = [[1, 6], [8, 10], [15, 18]]
-
标准求解,区间数组排序,单层循环,超过85%
import CheckFuncPerf as cfpdef merge_ext1(intervals):if not intervals:return []if len(intervals) == 1:return intervalsintervals.sort(key=lambda item: (item[0], item[1]))result = []tmpleft, tmpright = intervals[0][0], intervals[0][1]for index in range(1, len(intervals)):newleft, newright = intervals[index]if newleft > tmpright:result.append([tmpleft, tmpright])tmpleft = newlefttmpright = newrightelif newright > tmpright:tmpright = newrightresult.append([tmpleft, tmpright])return resultintervals = [[1, 3], [2, 6], [8, 10], [15, 18]] result = cfp.getTimeMemoryStr(merge_ext1, intervals) print(result['msg'], '执行结果 = {}'.format(result['result']))# 运行结果 函数 merge_ext1 的运行时间为 0.00 ms;内存使用量为 0.00 KB 执行结果 = [[1, 6], [8, 10], [15, 18]]
-
标准求解,单层循环,神之一刀,超越97%
比较再赋值改为单次max计算
import CheckFuncPerf as cfpdef merge_ext2(intervals):if not intervals:return []if len(intervals) == 1:return intervalsintervals.sort(key=lambda item: (item[0]))result = []tmpleft, tmpright = intervals[0][0], intervals[0][1]for index in range(1, len(intervals)):newleft, newright = intervals[index]if newleft > tmpright:result.append([tmpleft, tmpright])tmpleft = newlefttmpright = newrightelse:tmpright = max(tmpright, newright)result.append([tmpleft, tmpright])return resultintervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
result = cfp.getTimeMemoryStr(merge_ext2, intervals)
print(result['msg'], '执行结果 = {}'.format(result['result']))# 运行结果
函数 merge_ext2 的运行时间为 1.00 ms;内存使用量为 4.00 KB 执行结果 = [[1, 6], [8, 10], [15, 18]]
-
探索第三方排序,失败 采用numpy排序,不作不死,超过6%, 内存也爆
import CheckFuncPerf as cfpdef merge_ext3(intervals):if not intervals:return []if len(intervals) == 1:return intervalsimport numpy as nparr = np.array(intervals)tmplist = np.sort(arr, 0).tolist()result = []tmpleft, tmpright = tmplist[0][0], tmplist[0][1]for index in range(1, len(tmplist)):newleft, newright = tmplist[index]if newleft > tmpright:result.append([tmpleft, tmpright])tmpleft = newlefttmpright = newrightelif newright > tmpright:tmpright = newrightresult.append([tmpleft, tmpright])return resultintervals = [[1, 3], [2, 6], [8, 10], [15, 18]] result = cfp.getTimeMemoryStr(merge_ext3, intervals) print(result['msg'], '执行结果 = {}'.format(result['result']))# 运行结果 函数 merge_ext3 的运行时间为 116.01 ms;内存使用量为 14944.00 KB 执行结果 = [[1, 6], [8, 10], [15, 18]]
一日练,一日功,一日不练十日空
may the odds be ever in your favor ~