-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path850.employee-free-time.py
140 lines (126 loc) · 4.06 KB
/
850.employee-free-time.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Tag: Sweep Line
# Time: O(NlogN)
# Space: O(N)
# Ref: Leetcode-759
# Note: -
# 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.
#
# The `Intervals` is an 1d-array.
# Each two numbers shows an interval.
# For example, `[1,2,8,10]` represents that the employee works in `[1,2]` and `[8,10]`.
#
# Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.
#
# **Example 1:**
# ```
# Input:schedule = [[1,2,5,6],[1,3],[4,10]]
# Output:[(3,4)]
# Explanation:
# There are a total of three employees, and all common
# free time intervals would be [-inf, 1], [3, 4], [10, inf].
# We discard any intervals that contain inf as they aren't finite.
# ```
#
#
# **Example 2:**
# ```
# Input:schedule = [[1,3,6,7],[2,4],[2,5,9,12]]
# Output:[(5,6),(7,9)]
# Explanation:
# There are a total of three employees, and all common
# free time intervals would be [-inf, 1], [5, 6], [7, 9],[12,inf].
# We discard any intervals that contain inf as they aren't finite.
# ```
#
# 1.schedule and schedule[i] are lists with lengths in range [1, 100].
# 2.0 <= schedule[i].start < schedule[i].end <= 10^8.
from typing import (
List,
)
from lintcode import (
Interval,
)
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
"""
@param schedule: a list schedule of employees
@return: Return a list of finite intervals
"""
def employee_free_time(self, schedule: List[List[int]]) -> List[Interval]:
# Write your code here
works = []
for s in schedule:
for i in range(0, len(s), 2):
works.append((s[i], 'start'))
works.append((s[i + 1], 'end'))
works.sort()
count = 0
low = None
res = []
for i in range(len(works)):
if works[i][1] == 'start':
count += 1
if count == 1 and low is not None:
if low < works[i][0]:
res.append(Interval(low, works[i][0]))
else:
count -= 1
if count == 0:
low = works[i][0]
return res
class Solution:
"""
@param schedule: a list schedule of employees
@return: Return a list of finite intervals
"""
def employee_free_time(self, schedule: List[List[int]]) -> List[Interval]:
# Write your code here
works = []
for s in schedule:
for i in range(0, len(s), 2):
works.append([s[i], s[i + 1]])
works.sort()
merge = []
for inter in works:
if len(merge) == 0 or merge[-1][1] < inter[0]:
merge.append(inter)
else:
merge[-1][1] = max(merge[-1][1], inter[1])
res = []
for i in range(1, len(merge)):
res.append(Interval(merge[i - 1][1], merge[i][0]))
return res
from collections import defaultdict
class Solution:
"""
@param schedule: a list schedule of employees
@return: Return a list of finite intervals
"""
def employee_free_time(self, schedule: List[List[int]]) -> List[Interval]:
# Write your code here
line = defaultdict(int)
for i in range(len(schedule)):
for j in range(0, len(schedule[i]), 2):
line[schedule[i][j]] += i + 1
line[schedule[i][j + 1]] -= i + 1
prefix = 0
start = -1
res = []
for t in sorted(line.keys()):
if prefix == 0 and start != -1:
res.append(Interval(start, t))
start = -1
prefix += line[t]
if prefix == 0 and start == -1:
start = t
return res