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.
/**
* 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;
if(intervals.length == 1) return 1;
Comparator<Interval> comp = new Comparator<Interval>(){
@Override
public int compare(Interval a, Interval b){
if(a.start == b.start) return a.end - b.end;
return a.start - b.start;
}
};
Arrays.sort(intervals, comp);
Comparator<Interval> comp2 = new Comparator<Interval>(){
@Override
public int compare(Interval a, Interval b){
return a.end - b.end;
}
};
PriorityQueue<Interval> queue = new PriorityQueue<>(10, comp2);
int count =1;
queue.offer(intervals[0]);
for(int i=1; i< intervals.length; i++){
Interval top = queue.peek();
Interval next = intervals[i];
if(next.start >= top.end){
queue.poll();
}else{
count++;
}
queue.offer(next);
}
return count;
}
}
improve 1 : for comp2, you only need to save the ending time queue, improve 2 : you only need to measure the queue size in the end.