NowCoder-147 主持人调度(二)
Table of Contents
NowCoder-147 主持人调度(二) #
Solution 1 #
贪心. 每个活动开始时, 看能否从结束时间最早的活动处接上. 用一个堆来维护正在进行的活动. 整个过程中, 堆的最大大小即为最少需要的主持人数.
代码如下:
import heapq
class Solution:
def minmumNumberOfHost(self , n: int, startEnd: List[List[int]]) -> int:
startEnd.sort()
heap = []
ans = 1
for l, r in startEnd:
if heap and heap[0] <= l:
heapq.heappop(heap)
heapq.heappush(heap, r)
ans = max(ans, len(heap))
return ans