"""
# 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]':
intervals = []
for interval in schedule:
for i in interval:
intervals.append([i.start, i.end])
intervals.sort()
stack = [intervals[0]]
for i in range(1, len(intervals)):
prevInterval = stack.pop()
newInterval = intervals[i]
if prevInterval[1] >= newInterval[0]:
newInterval = [min(prevInterval[0], newInterval[0]), max(prevInterval[1], newInterval[1])]
else:
stack.append(prevInterval)
stack.append(newInterval)
if len(stack) <= 1:
return []
else:
ans = []
for i in range(1, len(stack)):
ans.append(Interval(start=stack[i-1][1], end=stack[i][0]))
return ans