253. Meeting Rooms II

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.

For example,

Given [[0, 30],[5, 10],[15, 20]],
return 2.

一刷
题解:

  1. 首先根据start,ascending对array排序。
  2. 然后创建heap, end最小的在root, heap中的第一个元素为array[0]
  3. 然后遍历array中的其它element, array[i]
    已知array[I].start>root.start
    如果array[I].start > root.end, 则合并(一个meeting root)。
    否则,加入heap中。
  4. 最后返回heap的size
/**
 * 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; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if(intervals == null || intervals.length == 0) return 0;
        
         // Sort the intervals by start time
        Arrays.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.start - b.start;
            }
        });
        
        // Use a min heap to track the minimum end time of merged intervals
        PriorityQueue heap = new PriorityQueue(intervals.length, 
           new Comparator() {
        public int compare(Interval a, Interval b) { return a.end - b.end; }
    });
        
        //start with the first meeting, put it to a meeting room
        heap.offer(intervals[0]);
        
        for(int i=1; i= inter.end){
                //there is no need for a new room, merge the interval
                inter.end = intervals[i].end;
            }
            else{
                heap.offer(intervals[i]);
            }
            heap.offer(inter);
        }
        
        return heap.size();
    }
}

二刷
首先根据起始时间排序。然后构建heap, 结束时间最早的在heap,如果当前最小的start都比top的end要大,可以共享一个room

/**
 * 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; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        int num = 0;
        if(intervals == null || intervals.length==0) return 0;
        if(intervals.length == 1) return 1;
        Arrays.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){ return a.start - b.start;}
        });
        
        
        //end small will exist on top
        PriorityQueue heap = new PriorityQueue(intervals.length, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.end - b.end;
            }
        });
        heap.offer(intervals[0]);
        for(int i=1; i=inter.end){
                inter.end = intervals[i].end;//combine
            }
            else{
                heap.offer(intervals[i]);
            }
            heap.offer(inter);
        }
        return heap.size();
        
    }
}

三刷
同上

/**
 * 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; }
 * }
 */
public 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 heap = new PriorityQueue<>(intervals.length, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.end - b.end;
            }
        });
        heap.offer(intervals[0]);
        for(int i=1; i=heap.peek().end){//combine
                Interval cur = heap.poll();
                cur.end = intervals[i].end;
                heap.offer(cur);
            }else{
                heap.offer(intervals[i]);
            }
        }
        return heap.size();   
    }
}

四刷

greedy

class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        Arrays.sort(intervals, (a, b)->(a.start - b.start));
        
        PriorityQueue q = new PriorityQueue<>(intervals.length, (a, b)->(a.end - b.end));
        q.offer(intervals[0]);
        int res = 1;
        for(int i=1; i

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