Meeting Rooms:
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.
For example,
Given[[0, 30],[5, 10],[15, 20]]
,
return2
.
Solution:
- sort the intervals ascending by start time.
- Create min heap to go thru each meeting interval and store the end times
- for each element in min_heap sweeping iteration
- push the end time of the given meeting interval
- when the minimum element of the heap is smaller than given start time, it means that min endtime meeting already ended
- heappop min until min endtime > current start time
- count the size of heap, max of this size over time is # of rooms required
TIME COMPLEXITY: O(n log k) where k is the # of rooms required
SPACE COMPLEXITY: O( k )
import heapq
class Solution(object):
def minMeetingRooms(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
intervals = sorted(intervals, key= lambda interval: interval.start)
# print map(lambda interval: interval.start, intervals)
endtimes = []
heapq.heapify(endtimes)
n = len(intervals)
rooms = 0
for i in range(n):
heapq.heappush(endtimes, intervals[i].end)
while endtimes[0] <= intervals[i].start:
heapq.heappop(endtimes)
rooms = max(rooms, len(endtimes))
return rooms