LeetCode 253. Meeting Rooms II 2019-07-18

题目描述

给定一个会议时间安排的数组,每个会议时间都会包括开始和结束的时间 [[s1,e1],[s2,e2],…] (si < ei),为避免会议冲突,同时要考虑充分利用会议室资源,请你计算至少需要多少间会议室,才能满足这些会议安排。

tag

贪心 堆

样例

1
2
输入: [[0, 30],[5, 10],[15, 20]]
输出: 2

算法1

(小顶堆) O(nlogn)
思路

https://leetcode-cn.com/problems/meeting-rooms-ii/solution/hui-yi-shi-ii-by-leetcode/

复杂度分析:
python 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:

if not intervals:
return 0

free_rooms = []

intervals.sort(key = lambda x: x[0])

heapq.heappush(free_rooms, intervals[0][1])

for i in intervals[1:]:

if free_rooms[0] <= i[0]:
heapq.heappop(free_rooms)

heapq.heappush(free_rooms, i[1])

return len(free_rooms)