-
Notifications
You must be signed in to change notification settings - Fork 17
/
meeting-rooms-ii.py
55 lines (41 loc) · 1.44 KB
/
meeting-rooms-ii.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
import heapq
from collections import deque
from typing import List, Tuple
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
tmp: List[Tuple[int, int]] = []
for start, end in intervals:
tmp.append((start, 1))
tmp.append((end, -1))
tmp.sort()
rooms = 0
required = 0
for _, adj in tmp:
rooms += adj
required = max(rooms, required)
return required
def minMeetingRooms(self, intervals):
rooms = []
result = 0
for interval in sorted(intervals):
if rooms and rooms[0] <= interval[0]:
while rooms and rooms[0] <= interval[0]:
heapq.heappop(rooms)
heapq.heappush(rooms, interval[1])
result = max(result, len(rooms))
return result
def minMeetingRoomsOptimizedBruteforce(self, intervals):
# Runs in ~ N * number of rooms time in average case
queue = deque()
result = 0
for interval in sorted(intervals):
while queue:
if queue[0][1] <= interval[0]:
queue.popleft()
elif queue[-1][1] <= interval[0]:
queue.pop()
else:
break
queue.append(interval)
result = max(result, len(list(filter(lambda x: x[1] > interval[0], queue))))
return result