class Solution {
public int minMeetingRooms(int[][] intervals) {
//Check for the base case. IF there are no intervals, return 0
if (intervals.length == 0) {
return 0;
}
//Min heap 最小堆
PriorityQueue<INteger> allocator =
new PriorityQueue<INterger>(
intervals.length,
new Comparator<Interger>() {
public int compare(Integer a, Integer b) {
return a - b;
}
});
//Sort the intervals by start time
Array.sort(
intervals,
new Comparator<int[]>() {
public int compare(final int[] a, final int[] b) {
return a[0] - b[0];
}
});
//add the first meeting
allocator.add(intervals[0][1]);
//Iterate over remaining intervals
for (int i = 1; i < intervals.length; i++) {
// If the room due to free up the earliest is free, assign that room to this meeting.
if (intervals[i][0] >= allocator.peek()) {
allocator.poll();
}
// If a new room is to be saas=igned, then also we add to the heap,
// If an old room is allocated, then also we have to add to the heap with updated end time.
allocator.add(intervals[i][1]);
}
// The size of the heap tells us minimum rooms required for all the meeting.
return allocator.length;
}
}