Python Simulation Solution with PriorityQueue


  • 0
    Y
    from Queue import PriorityQueue
    class Solution(object):
        def minMeetingRooms(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: int
            """
            if not intervals:
                return 0
            intervals = sorted(intervals, key = lambda a: a.start) # Sorted by start time.
            count = 1
            available_rooms = PriorityQueue()
            first_room = intervals[0]
            available_rooms.put((first_room.end, first_room.start))
            
            for index in range(1, len(intervals)):
                meeting = intervals[index]
                soonest_available_room = available_rooms.get()
                if soonest_available_room[0] <= meeting.start:
                    available_rooms.put((meeting.end, meeting.start))
                else:
                    # If the room with the soonest finish time doesn't satisfy,
                    # have to create a new room.
                    count += 1
                    available_rooms.put(soonest_available_room)
                    available_rooms.put((meeting.end, meeting.start))
            return count
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.