-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMeetingRoomII.java
More file actions
64 lines (52 loc) · 1.67 KB
/
MeetingRoomII.java
File metadata and controls
64 lines (52 loc) · 1.67 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
55
56
57
58
59
60
61
62
63
64
import java.util.*;
public class MeetingRoomII {
public static void main(String[] args) {
Interval[] its = new Interval[3];
its[0] = new Interval(0, 30);
its[1] = new Interval(5, 10);
its[2] = new Interval(15, 20);
MeetingRoomII mr = new MeetingRoomII();
int minRooms = mr.minMeetingRooms(its);
System.out.println(minRooms);
}
public int minMeetingRooms(Interval[] intervals) {
if(intervals.length <= 1) return intervals.length;
Arrays.sort(intervals, new Comparator<Interval> () {
@Override
public int compare(Interval i1, Interval i2) {
// if(i1.start != i2.start) return i1.end - i2.end;
return i1.start - i2.start;
}
});
PriorityQueue<Interval> pq = new PriorityQueue<> (new Comparator<Interval> () {
@Override
public int compare(Interval i1, Interval i2) {
return i1.end - i2.end;
}
});
pq.offer(intervals[0]);
int room = 1;
for(int i=1; i<intervals.length; ++i) {
Interval prev = pq.poll();
Interval curr = intervals[i];
// collision detected, need one more room
if(curr.start < prev.end) {
room += 1;
pq.offer(curr);
pq.offer(prev);
} else {
prev.end = curr.end;
pq.offer(prev);
}
}
return pq.size();
}
static class Interval {
int start;
int end;
public Interval(int s, int e) {
start = s;
end = e;
}
}
}