Python heap solution with comments.

  • 22
    def minMeetingRooms(self, intervals):
        intervals.sort(key=lambda x:x.start)
        heap = []  # stores the end time of intervals
        for i in intervals:
            if heap and i.start >= heap[0]: 
                # means two intervals can use the same room
                heapq.heapreplace(heap, i.end)
                # a new room is allocated
                heapq.heappush(heap, i.end)
        return len(heap)

  • 0

    Nevermind. I missed the first sorting part. You are right.

  • 0

    great solution

  • 0

    Thank you. Your codes are always clean and easy to follow, I have been learning a lot from your posts.

  • 0

    This should be among the top 5 solutions. Very easy to understand. Thanks again @caikehe !

Log in to reply

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