Meeting Rooms II

题目描述

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

For example, Given [[0, 30],[5, 10],[15, 20]], return 2.

解题方法

有点类似airline的那道题

Solution

from heapq import *
# Definition for an interval.
# class Interval(object):
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e

class Solution(object):
    def minMeetingRooms(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: int
        """
        if not intervals:
            return 0

        length = len(intervals)
        def compare(a, b):
            return a.start - b.start

        intervals = sorted(intervals, cmp=compare)

        curNum = 0
        maxNum = 0
        endTimes = []
        for i in range(length):
            curNum += 1
            heappush(endTimes, intervals[i].end)
            while endTimes and endTimes[0] <= intervals[i].start:
                heappop(endTimes)
                curNum -= 1
            if curNum > maxNum:
                maxNum = curNum
        return maxNum

Reference