Python solution by sorting start and end events


  • 0
    S
    class Solution(object):
        def minMeetingRooms(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: int
            """
            # Keep start and end times as Events and sort them by event timestamp
            events = []
            for i in intervals:
                events.append((i.start, True))
                events.append((i.end, False))
            events.sort()
            nrooms = 0
            maxrooms = 0
            for ev,start in events:
                if start:
                    nrooms += 1
                    if nrooms > maxrooms:
                        maxrooms = nrooms
                else:
                    nrooms -= 1
            return maxrooms
    

Log in to reply
 

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