Python heap solution with comments.


  • 22
    C
    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)
            else:
                # a new room is allocated
                heapq.heappush(heap, i.end)
        return len(heap)

  • 0
    X

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


  • 0
    T

    great solution


  • 0
    G

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


  • 0
    R

    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.