Leetcode: 759. Employee Free Time

Problem Statement

"""
# 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]":
        last = None
        ans = []
        for i in merge(*schedule, key=lambda i: (i.start, i.end)):
            if last is None:
                last = i.end
            elif i.start <= last:
                last = max(last, i.end)
            else:
                ans.append(Interval(last, i.start))
                last = i.end
        return ans