Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Constraints:
1 <= intervals.length <= 104
0 <= starti < endi <= 106
1 现将intervals从大到小排序(sort intervals),一个一个检查开会结束时间
2 找出之前会议最早结束时间(minHeap),比较当前要开的会议开始时间是否大于等于最早结束时间。如果是,则不用增加会议室,否则会议室+1.
Time complexityO(nlogn), space complexityO(n)
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
res = 1
intervals.sort()
pre_ends = [intervals[0][1]]
for start, end in intervals[1:]:
if len(pre_ends) > 0:
pre_end = pre_ends[0]
if pre_end is not None and start < pre_end:
res += 1
elif pre_end is not None and start >= pre_end:
heapq.heappop(pre_ends)
heapq.heappush(pre_ends, end)
return res