Problem – Find Overlapping Pipeline Intervals
Find all time intervals during which at least two pipelines are running at the same time.
You are given a list of half-open or closed intervals (assume closed here) where each interval[start, end] represents the running time of one pipeline.
Return all sub-intervals where the number of running pipelines is ≥ 2.
Example
Input:[[2, 5], [12, 15], [4, 8]]
Output:[[4, 5]]
Because between time 4 and 5, both the interval [2, 5] and [4, 8] are running.
思路总结(Brief Idea)
- 把每个区间拆成两个“事件”:
(start, +1)和(end, -1)。 - 按时间排序(同一时间先处理 +1 再处理 -1)。
- 从左到右扫一遍,用
running记录当前运行中的 pipeline 数。- 当
running从 1 变成 2 时,记录重叠区间的开始时间。 - 当
running从 2 变成 1 时,记录结束时间并加入结果。
- 当
- 时间复杂度:排序 O(n log n),扫描 O(n)。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Meta、Google、Amazon 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。