python solution using heap


  • 0

    The idea is to use a min heap to maintain the earliest ending meeting.
    The size of the heap is the number of rooms needed.

    class Solution(object):
        def minMeetingRooms(self, intervals):
            ans = 0
            intervals = [(i.start, i.end) for i in intervals]
            intervals.sort()
            rooms = []
            
            for s, e in intervals:
                if rooms and rooms[0] <= s:
                    heapq.heappop(rooms)
                heapq.heappush(rooms, e)
                ans = max(ans, len(rooms))
                
            return ans
            
    

Log in to reply
 

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