-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeeting_rooms_2.py
More file actions
34 lines (26 loc) · 913 Bytes
/
meeting_rooms_2.py
File metadata and controls
34 lines (26 loc) · 913 Bytes
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
# https://www.lintcode.com/problem/meeting-rooms-ii/description
# Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei),
# find the minimum number of conference rooms required.
"""
>>> minMeetingRooms([Interval(5, 10), Interval(15, 20), Interval(0, 30)])
2
>>> minMeetingRooms([Interval(7, 10), Interval(2, 4)])
1
"""
import heapq
class Interval:
def __init__(self, start=0, end=0):
self.start = start
self.end = end
def minMeetingRooms(intervals):
if not intervals:
return 0
intervals.sort(key=lambda interval: interval.start)
first_interval = intervals[0]
min_heap = [first_interval.end]
for i in range(1, len(intervals)):
current = intervals[i]
if current.start >= min_heap[0]:
heapq.heappop(min_heap)
heapq.heappush(min_heap, current.end)
return len(min_heap)