algorithm - 在给定范围内查找缺失范围
问题描述
当给出主范围并给出所有子范围时,我们需要找到缺失的范围。主要范围:[-10, 10]
子范围: [-10, -5] , [-4, -3], [-2, 3], [7, 10]
假设:
1) 范围值可以达到 2^63。
2)子范围不会重叠,它们的顺序可以不同。例如:可以是 [-10, -5],[7, 10], [-2, 3], [-4, -3]
在这里找到缺失范围的最佳算法是什么?
解决方案
假设区间是未排序的,我看不到避免排序成本,因为每个区间都可以是单例 ( [n,n]
)。该成本可以O(n log n)
用于比较排序或O(n)
基数排序。从现在开始,让我们假设输入区间已排序并且不包含重叠。这是一个O(n)
单程 Python 实现:
xs = [[-10, -5] , [-4, -3], [-2, 3], [7, 10]]
bounds = (-10, 10)
missing = list()
# pre-processing
xs_sorted = sorted(xs)
# pre-processing a missing range on the lower bound
if bounds[0] < xs_sorted[0][0]:
missing.append((bounds[0], xs_sorted[0][0]-1))
def f_reduce(a, b):
if a[1] + 1 == b[0]:
# merge contiguous intervals
return (a[0], b[1])
else:
# gap detected; add the gap to the missing range list
# and move to the next value
missing.append((a[1]+1, b[0]-1))
return b
from functools import reduce
reduce(f_reduce, xs_sorted)
# post-processing on a missing range on the upper bound
if bounds[1] > xs_sorted[-1][1]:
missing.append((xs_sorted[-1][1]+1, bounds[1]))
print(missing)
# [(4, 6)]
方法是使用reduce
带有臭味副作用的功能样式。当函数f_reduce
遇到两个区间(a, b)
and(c, d)
时,我们返回一个复合区间(a, d)
if b + 1 == c
。否则,检测并存储间隙;返回的区间是(c, d)
。当间隔的两个极端范围内出现间隙时,预处理和后处理步骤正在处理令人讨厌的情况。
推荐阅读
- sql - 根据与 BigQuery 中其他行的相似性过滤行
- elasticsearch - 根据“matched_queries”的数量过滤 Elasticsearch 结果
- r - 正则表达式用于提取两个数字之间和结束模式
- docker - 如何创建一个 Airflow 任务,在其中我启动一个支持 GPU 的 Docker 容器
- python - 如何从 Python 中的 URL 中删除 .com 和“https://”之后的字符串
- pyqt - 为什么我不能在 pyqt5 中使用键盘监听器?
- javascript - 尝试从递归函数返回值时出现“未定义”
- java - 如何获取 JUnit 测试套件类的注解?
- powershell - 最后的 For each 循环不会产生任何输出 | 电源外壳
- list - scala spark减少groupby中的列表