BM96 主持人调度(二)
方法一: 用堆模拟活动过程,堆最大长度则为所需最少主持人
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 计算成功举办活动需要多少名主持人
# @param n int整型 有n个活动
# @param startEnd int整型二维数组 startEnd[i][0]用于表示第i个活动的开始时间,startEnd[i][1]表示第i个活动的结束时间
# @return int整型
from queue import PriorityQueue
class Solution:
def minmumNumberOfHost(self , n: int, startEnd: List[List[int]]) -> int:
# write code here
que = PriorityQueue()
times = sorted(startEnd,key=lambda x : (x[0], x[1]))
#print(times)
start, end = times[0]
cur_end = end
needhost = 1
for i in range(1, len(times)):
start, end = times[i]
if start >= cur_end:
que.put(end,end)
cur_end = que.get()
else:
cur_end, end = min(cur_end, end), max(cur_end, end)
que.put(end,end)
needhost += 1
return needhost
方法二:按时刻统计在进行活动数目,最大值等于所需最少主持人数