Advertisement
hoangreal

Return list of intervals employee free time

Oct 21st, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. """
  2. We are given a list schedule of employees, which represents the working time for each employee.
  3.  
  4. Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.
  5.  
  6. Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
  7. """
  8.  
  9. # Definition for an Interval.
  10. # class Interval:
  11. #     def __init__(self, start: int = None, end: int = None):
  12. #         self.start = start
  13. #         self.end = end
  14.  
  15. class Solution:
  16.     def employeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]':
  17.         # Flattening the schedule
  18.         intervals = [interval for employee in schedule for interval in employee]
  19.         # Sorting by start of each Interval
  20.         intervals.sort(key=lambda x: x.start)
  21.         res, end = [], intervals[0].end
  22.         # Checking for free time between intervals
  23.         for i in intervals[1:]:
  24.             if end < i.start:
  25.                 res.append(Interval(end, i.start))
  26.             end = max(end, i.end)
  27.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement