-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path253-meeting-rooms-ii.py
56 lines (44 loc) · 1.31 KB
/
253-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
56
from heapq import *
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
intervals.sort()
heap = []
for s, e in intervals:
if heap and heap[0] <= s:
heappop(heap)
heappush(heap, e)
return len(heap)
# time O(nlogn)
# space O(n)
# using array and line sweep and heap to store previous intervals’ states
from heapq import *
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
intervals.sort()
heap = []
res = 0
for s, e in intervals:
while heap and heap[0] <= s:
heappop(heap)
heappush(heap, e)
res = max(res, len(heap))
return res
# time O(nlogn)
# space O(n)
# using array and line sweep and heap to store previous intervals’ states
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
events = []
for s, e in intervals:
events.append((s, 1))
events.append((e, - 1))
events.sort()
status = 0
res = 0
for timestamp, weight in events:
status += weight
res = max(res, status)
return res
# time O(nlogn)
# space O(n)
# using array and line sweep and sort