题解 | #主持人调度(二)#
主持人调度(二)
https://www.nowcoder.com/practice/4edf6e6d01554870a12f218c94e8a299
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算成功举办活动需要多少名主持人
* @param n int整型 有n个活动
* @param startEnd int整型二维数组 startEnd[i][0]用于表示第i个活动的开始时间,startEnd[i][1]表示第i个活动的结束时间
* @return int整型
*/
public int minmumNumberOfHost (int n, int[][] startEnd) {
Arrays.sort(startEnd,new Comparator<int[]>(){
public int compare(int[] o1,int[] o2){
if(o1[0] < o2[0]){
return -1 ;
}else if(o1[0] > o2[0]){
return 1 ;
}else{
return 0 ;
}
}
}) ;
PriorityQueue<Integer> enQue = new PriorityQueue<>() ;
int ans = 0 ;
for(int[] stEnd:startEnd){
while(enQue.size() > 0 && enQue.peek() <= stEnd[0]){
enQue.poll() ;
}
enQue.add(stEnd[1]) ;
ans = Math.max(ans,enQue.size()) ;
}
return ans ;
// write code here
}
}
按照start排序,然后遍历所有区间,用堆记录结束位置,小于当前的开始位置的活动即在当前已经结束,可以释放主持人

