759. 员工空闲时间

给定员工的 schedule 列表,表示每个员工的工作时间。
每个员工都有一个非重叠的时间段  Intervals 列表,这些时间段已经排好序。
返回表示 所有 员工的 共同,正数长度的空闲时间 的有限时间段的列表,同样需要排好序。
示例 1:
输入:schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]] 输出:[[3,4]] 解释: 共有 3 个员工,并且所有共同的 空间时间段是 [-inf, 1], [3, 4], [10, inf]。 我们去除所有包含 inf 的时间段,因为它们不是有限的时间段。
示例 2:
输入:schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]] 输出:[[5,6],[7,9]]
(尽管我们用 [x, y] 的形式表示 Intervals ,内部的对象是 Intervals 而不是列表或数组。例如,schedule[0][0].start = 1, schedule[0][0].end = 2,并且 schedule[0][0][0] 是未定义的)
而且,答案中不包含 [5, 5] ,因为长度为 0。

法 1

题解
""" # Definition for an Interval. class Interval: def __init__(self, start: int = None, end: int = None): self.start = start self.end = end """ class Solution: def employeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]': points = [] for worker in schedule: for itv in worker: points.append([itv.start, 1]) points.append([itv.end, -1]) points.sort(key=lambda x:(x[0], -x[1])) res = [] cnt = 0 start, end = None, None for p in points: cnt += p[1] if cnt == 0 and start is None: start = p[0] if cnt == 1 and start is not None: end = p[0] res.append(Interval(start, end)) start, end = None, None return res
思路