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.
一刷
题解:
- 首先根据start,ascending对array排序。
- 然后创建heap, end最小的在root, heap中的第一个元素为array[0]
- 然后遍历array中的其它element, array[i]
已知array[I].start>root.start
如果array[I].start > root.end, 则合并(一个meeting root)。
否则,加入heap中。 - 最后返回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<Interval>(){
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<Interval> heap = new PriorityQueue<Interval>(intervals.length,
new Comparator<Interval>() {
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<intervals.length; i++){
//get the meeting room that finishes earliest
Interval inter = heap.poll();
if(intervals[i].start >= 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<Interval>(){
public int compare(Interval a, Interval b){ return a.start - b.start;}
});
//end small will exist on top
PriorityQueue<Interval> heap = new PriorityQueue<Interval>(intervals.length, new Comparator<Interval>(){
public int compare(Interval a, Interval b){
return a.end - b.end;
}
});
heap.offer(intervals[0]);
for(int i=1; i<intervals.length; i++){
Interval inter = heap.poll();
if(intervals[i].start>=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<Interval>(){
public int compare(Interval a, Interval b){
return a.start - b.start;
}
});
PriorityQueue<Interval> heap = new PriorityQueue<>(intervals.length, new Comparator<Interval>(){
public int compare(Interval a, Interval b){
return a.end - b.end;
}
});
heap.offer(intervals[0]);
for(int i=1; i<intervals.length; i++){
if(intervals[i].start>=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<Interval> q = new PriorityQueue<>(intervals.length, (a, b)->(a.end - b.end));
q.offer(intervals[0]);
int res = 1;
for(int i=1; i<intervals.length; i++){
Interval cur = q.peek();
if(cur.end<=intervals[i].start){
cur.end = intervals[i].end;
}else{
q.offer(intervals[i]);
}
}
return q.size();
}
}