-
Notifications
You must be signed in to change notification settings - Fork 0
/
Employee Free Time
39 lines (30 loc) · 1.38 KB
/
Employee Free Time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'''We are given a list schedule of employees, which represents the working time for each employee.
Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.
Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
(Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays.
For example, schedule[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] is not defined).
Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.'''
"""
# Definition for an Interval.
class Interval(object):
def __init__(self, start=None, end=None):
self.start = start
self.end = end
"""
class Solution(object):
def employeeFreeTime(self, schedule):
"""
:type schedule: [[Interval]]
:rtype: [Interval]
"""
s = sorted([j for i in schedule for j in i], key=lambda x:x.start)
final = []
for intl in s:
if final and intl.start<=final[-1].end:
final[-1].end = max(final[-1].end, intl.end)
else:
final.append(intl)
free = []
for a in range(1, len(final)):
free.append(Interval(final[a-1].end, final[a].start))
return(free)