-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCP-21-20190307.py
29 lines (20 loc) · 950 Bytes
/
DCP-21-20190307.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
"""
This problem was asked by Snapchat.
Given an array of time intervals (start, end) for classroom lectures (possibly overlapping),
find the minimum number of rooms required.
For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
"""
class ClassTimes:
def __init__(self, times):
self.start = times[0]
self.end = times[1]
def __repr__(self):
return str((self.start, self.end))
def overlapping(self, ct):
return (self.start > ct.start and self.start < ct.end) or (self.end > ct.start and self.end < ct.end)
def solve(times):
return max([sum(1 for other_class in times if current_class.overlapping(other_class)) for current_class in times])
times = list(map(ClassTimes, [(30, 75), (0, 50), (60, 150), (40, 200), (80, 100), (210, 220), (0, 30), (0, 50)]))
print(sorted(times, key=lambda ct: ct.start))
rooms_needed = solve(times)
print(rooms_needed)