-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
meeting-rooms-iii.py
55 lines (48 loc) · 1.47 KB
/
meeting-rooms-iii.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
# Time: O(mlogm + n + mlogn)
# Space: O(n)
import heapq
# one heap solution
class Solution(object):
def mostBooked(self, n, meetings):
"""
:type n: int
:type meetings: List[List[int]]
:rtype: int
"""
meetings.sort()
min_heap = [(meetings[0][0], i) for i in xrange(n)]
result = [0]*n
for s, e in meetings:
while min_heap and min_heap[0][0] < s:
_, i = heapq.heappop(min_heap)
heapq.heappush(min_heap, (s, i))
e2, i = heapq.heappop(min_heap)
heapq.heappush(min_heap, (e2+(e-s), i))
result[i] += 1
return max(xrange(n), key=lambda x:result[x])
# Time: O(mlogm + n + mlogn)
# Space: O(n)
import heapq
# two heaps solution
class Solution2(object):
def mostBooked(self, n, meetings):
"""
:type n: int
:type meetings: List[List[int]]
:rtype:
"""
meetings.sort()
unused, used = range(n), []
result = [0]*n
for s, e in meetings:
while used and used[0][0] <= s:
_, i = heapq.heappop(used)
heapq.heappush(unused, i)
if unused:
i = heapq.heappop(unused)
heapq.heappush(used, (e, i))
else:
e2, i = heapq.heappop(used)
heapq.heappush(used, (e2+(e-s), i))
result[i] += 1
return max(xrange(n), key=lambda x:result[x])