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:

  1. sort the intervals ascending by start time.
  2. Create min heap to go thru each meeting interval and store the end times
  3. for each element in min_heap sweeping iteration
    1. push the end time of the given meeting interval
    2. when the minimum element of the heap is smaller than given start time, it means that min endtime meeting already ended
    3. heappop min until min endtime > current start time
    4. 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

results matching ""

    No results matching ""