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<Interval>(){
            public int compare(Interval a, Interval b){
                return a.start - b.start;
            }
        });
        PriorityQueue<Interval> minHeap = new PriorityQueue<Interval>(intervals.length, new Comparator<Interval>(){
            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();
    }
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/bde4e9f9b2b9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞