Meeting Rooms II

Interval
Medium

Approach

Diagrams

Analysis

Time

O(n log(n))

Space

O(n)
class Solution:
    def minMeetingRooms(self, intervals):
        startTimes = [startTime for startTime, _ in intervals]
        endTimes = [endTime for _, endTime in intervals]

        lastIdx = curIdx = 0
        numRoomsNeeded = numRoomsAvailable = 0

        startTimes.sort()
        endTimes.sort()

        while curIdx < len(startTimes):
            lastEnd, curStart = endTimes[lastIdx], startTimes[curIdx]

            if lastEnd > curStart:
                if numRoomsAvailable == 0:
                    numRoomsNeeded += 1

                else:
                    numRoomsAvailable -= 1

                curIdx += 1

            else:
                numRoomsAvailable += 1
                lastIdx += 1

        return numRoomsNeeded

Learn

#Greedy

Videos