-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path253_meeting_rooms_ii.java
More file actions
54 lines (45 loc) · 1.38 KB
/
253_meeting_rooms_ii.java
File metadata and controls
54 lines (45 loc) · 1.38 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
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.
Example 1:
Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
Example 2:
Input: [[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;
}
Arrays.sort(intervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
});
Queue<Integer> queue = new PriorityQueue<Integer>();
queue.offer(intervals[0].end);
int max_size = 1, size = 1, i = 1;
while(i < intervals.length) {
if (queue.isEmpty() || intervals[i].start < queue.peek()) {
size ++;
max_size = Math.max(max_size, size);
queue.offer(intervals[i].end);
i ++;
} else {
queue.poll();
size --;
}
}
return max_size;
}
}