Leetcode 761 Solution

This article provides solution to leetcode question 761 (employee-free-time)

https://leetcode.com/problems/employee-free-time

Solution

"""
# 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]':
        events = []
        for emp_schedule in schedule:
            for interval in emp_schedule:
                events.append((interval.start, -1))
                events.append((interval.end, 1))

        events.sort()

        print(events)

        cnt = 0
        last_free_ts = None
        ans = []
        for ts, op in events:
            if op == -1:
                cnt += 1
                if cnt == 1 and last_free_ts is not None and ts - last_free_ts >= 1:
                    ans.append(Interval(start=last_free_ts, end=ts))
            elif op == 1:
                cnt -= 1
                if cnt == 0:
                    last_free_ts = ts

        return ans