classSolution: defemployeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]': d = {} for emp in schedule: for interval in emp: start = interval.start end = interval.end if start notin d: d[start] = 0 d[start] += 1 if end notin d: d[end] = 0 d[end] -= 1 curr = 0 res = [] l = -1 for t insorted(d): curr += d[t] if curr == 0: if l == -1: l = t elif l != -1: res.append(Interval(l,t)) l = -1 return res
这个题解是用排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: defemployeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]': q = [] heapq.heapify(q) for idx, emp inenumerate(schedule): for sch in emp: heapq.heappush(q, (sch.start, idx, sch)) # 此时我们得到一个用start来排序的区间堆 res = [] current_end = q[0][2].end while q: top = heapq.heappop(q)[2] if top.start > current_end: res.append(Interval(current_end,top.start)) if current_end < top.end: current_end = top.end return res