253. Meeting Rooms II
Input: [[0, 30],[5, 10],[15, 20]]
Output: 2Input: [[7,10],[2,4]]
Output:1/**
* 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;
// sort the arrays by start time
Arrays.sort(intervals, new Comparator<Interval>(){
@Override
public int compare(Interval a, Interval b) {return a.start - b.start;}
});
// sort the heap by end time (scheduled meeting)
PriorityQueue<Interval> pq = new PriorityQueue<Interval>(intervals.length, new Comparator<Interval>(){
@Override
public int compare (Interval a, Interval b) {return a.end - b.end;}
});
pq.offer(intervals[0]);
for(int i = 1; i < intervals.length; i++){
Interval earliest = pq.poll();
Interval curInterval = intervals[i];
if(earliest.end <= curInterval.start){
earliest.end = curInterval.end; // merge (use the same room)
}
else {
pq.offer(curInterval); // schedule a new room
}
pq.offer(earliest);
}
return pq.size();
}
}Last updated
Was this helpful?