-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem21.py
43 lines (35 loc) · 1.1 KB
/
problem21.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
# 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.
import basics
import itertools
def needed_rooms(schedule):
input_data = [' '.join(str(j) for j in i) for i in schedule]
needed_rooms = 0
start_times = []
end_times = []
for c in schedule:
start_times.append(c[0])
end_times.append(c[1])
start_times.sort()
end_times.sort()
s = 0
e = 0
while(s < len(schedule)):
if(start_times[s] >= end_times[e]):
e += 1
else:
needed_rooms += 1
s += 1
return needed_rooms
def main():
num_of_classes = basics.getNumber()
schedule = []
for i in range(num_of_classes):
interval = basics.getNumberList()
if(len(interval) > 2):
raise Exception('Only 2 numbers per interval..')
schedule.append(interval)
print(needed_rooms(schedule))
if __name__ == '__main__':
main()