Thinking process
using a minHeap to save all possible rooms
find the room that end earliest, if the start time of current interval start after that, then this interval can fit in that room and update the room's end time at the same time
don't forget to put the room that polled out back to the minHeap
/**
* 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.length == 0) return 0;
int result = 1;
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval i, Interval j) {
return i.start - j.start;
}
});
PriorityQueue<Interval> rooms = new PriorityQueue<>(intervals.length, new Comparator<Interval>(){
public int compare(Interval i, Interval j) {
return i.end - j.end;
}
});
rooms.offer(intervals[0]);
for (int i = 1; i < intervals.length; ++i) {
Interval room = rooms.poll();
if (intervals[i].start >= room.end) {
room.end = intervals[i].end;
} else {
rooms.offer(intervals[i]);
}
rooms.offer(room);
}
return rooms.size();
}
}