程序媛詹妮弗
终身学习

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

Example 1:

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2

Example 2:

Input: [[7,10],[2,4]]
Output: 1

 

思路

1. Sort the intervals by starting time so it will always find the earliest available meeting once the most recent one is end.

2. keep track of the endings showing when there is a meeting room gets available.


 代码

public int minMeetingRooms(int[][] intervals) {
        if (intervals == null || intervals.length == 0) return 0; // corner case
    
        Arrays.sort(intervals, (int[]a, int[]b)->(a[0] - b[0]));  //掌握自定义sort的写法
PriorityQueue<Integer> minHeap = new PriorityQueue<>((a,b)-> a - b); minHeap.add(intervals[0][1]); for(int i =1; i<intervals.length;i++) { //尤其注意是初始是 i=1 if(intervals[i][0] >= minHeap.peek()) { minHeap.poll(); } minHeap.add(intervals[i][1]); } return minHeap.size(); }

  

 

posted on 2018-10-18 05:20  程序媛詹妮弗  阅读(272)  评论(0编辑  收藏  举报