题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是给一系列会议的开始时间和结束时间,以及会议室个数,现在规定,每个会议都会默认用当前空闲会议室中最小的,如果没有会议室空闲就延期开始,求使用最多的会议室编号。

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
rec = [0]*n
ans = 0
max_vis = 0
room = list(range(n))
heapq.heapify(room)
meetings.sort(key=lambda x : x[0])
on_meetings = []
heapq.heapify(on_meetings)
# print(meetings)

for start, end in meetings:
while on_meetings and on_meetings[0][0] <= start:
heappush(room, heappop(on_meetings)[1])
e = 0
if len(room) == 0:
e, r = heapq.heappop(on_meetings)
heapq.heappush(room, r)
end += e - start
nxt_room = heapq.heappop(room)
heapq.heappush(on_meetings, (end, nxt_room))
rec[nxt_room] += 1

return rec.index(max(rec))