Easier version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int minMeetingRooms(int[][] intervals) {
if (intervals == null || intervals.length == 0) return 0;
int n = intervals.length;
int[] start = new int[n];
int[] end = new int[n];
for (int i = 0; i < n; ++i) {
start[i] = intervals[i][0];
end[i] = intervals[i][1];
}
Arrays.sort(start);
Arrays.sort(end);

int res = 0, endIndex =0;
for (int i = 0; i < n; ++i) {
if (start[i] < end[endIndex]) res++;
else endIndex++;
}
return res;
}
}

Previous PriorityQueue version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int minMeetingRooms(int[][] intervals) {
if (intervals == null || intervals.length == 0) return 0;
Arrays.sort(intervals, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});

PriorityQueue<int[]> pq = new PriorityQueue(intervals.length, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
});

pq.offer(intervals[0]);
for (int i = 1; i < intervals.length; ++i) {
int[] interval = pq.poll();
if (interval[1] <= intervals[i][0]) {
interval[1] = intervals[i][1];
pq.offer(interval);
} else {
pq.offer(intervals[i]);
pq.offer(interval);
}

}

return pq.size();
}
}