253. Meeting Rooms II

Medium

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if (intervals == null || intervals.length == 0){
            return 0;
        }    
        Arrays.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.start - b.start;
            }
        });
        PriorityQueue minHeap = new PriorityQueue(intervals.length, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.end - b.end;
            } 
        });
        minHeap.offer(intervals[0]);
        for (int i = 1; i < intervals.length; i++){
            Interval interval = minHeap.poll();
            if (intervals[i].start >= interval.end){
                interval.end = intervals[i].end;
            } else {
                minHeap.offer(intervals[i]);
            }
            minHeap.offer(interval);
        }
        return minHeap.size();
    }
}

你可能感兴趣的:(253. Meeting Rooms II)