import heapq
class Solution:
def minMeetingRooms(self, intervals):
intervals.sort(key = lambda x: x[0])
# 维护一个最小堆, 存储当前的回会议结束的最小时间
min_heap = []
num = 0
for i in range(len(intervals)):
# 如果当前时间比最小时间大, 说明可以共用一个, 此时把根节点pop出来
while min_heap and intervals[i][0] >= min_heap[0]:
heapq.heappop(min_heap)
heapq.heappush(min_heap, intervals[i][1])
num = max(num, len(min_heap))
return num
# 排序+遍历比较(推荐使用)
def minMeetingRooms2(self, intervals):
start = list()
end = list()
# 分别得到活动起始时间
for i in range(len(intervals)):
start.append(intervals[i][0])
end.append(intervals[i][1])
# 分别对开始和结束时间排序
start.sort()
end.sort()
num = 0
j = 0
for i in range(len(intervals)):
# 新开始的节目大于上一轮结束的时间,主持人不变
if start[i] >= end[j]:
j = j+1
else:
# 主持人增加
num = num + 1
return num
intervals = [[5,10], [0,30],[15,20]]
Test = Solution()
print(Test.minMeetingRooms(intervals))
print(Test.minMeetingRooms2(intervals))
# res = 2
intervals = [[7,10],[2,4]]
print(Test.minMeetingRooms(intervals))
print(Test.minMeetingRooms2(intervals))
# res = 1
leetcode 会议室题解
最新推荐文章于 2024-08-12 14:00:01 发布