题解 | #主持人调度# LeetCode MeetingRoom [S-P1]

主持人调度

http://www.nowcoder.com/practice/4edf6e6d01554870a12f218c94e8a299

时间: O(NlogN) heap operation is O(logN)
空间: O(N)

import java.util.*;

public class Solution {
    public int minmumNumberOfHost (int n, int[][] startEnd) {
      // sort startEnd by start, then end.
      Comparator<int[]> comparator = (a, b) -> {
        // return a[1]-b[1]会int overflow。。。 是真TM无聊
        if (a[0] == b[0]) return a[1] < b[1] ? -1 : 1;
        else return a[0] < b[0] ? -1 : 1;
      };
      Arrays.sort(startEnd, comparator);
      
      PriorityQueue<Integer> endMinHeap = new PriorityQueue<>();
      
      for (int[] se : startEnd) {
        if (!endMinHeap.isEmpty() && endMinHeap.peek() <= se[0])
          endMinHeap.poll();  // remove ended meeting and use the same host
        endMinHeap.offer(se[1]);  // enqueue endtime of new meeting
      }
      
      return endMinHeap.size();
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务