-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeetingRoomProblem.java
More file actions
38 lines (36 loc) · 1.41 KB
/
meetingRoomProblem.java
File metadata and controls
38 lines (36 loc) · 1.41 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
class Solution {
public int minMeetingRooms(Interval[] intervals) {
if(intervals == null || intervals.length == 0)
return 0;
Arrays.sort(intervals, new Comparator<Interval> () {
@Override
public int compare (Interval i1, Interval i2)
{
return i1.start - i2.start;
}
});
PriorityQueue<Interval> heap = new PriorityQueue<Interval>(intervals.length,new Comparator<Interval>()
{
@Override
public int compare(Interval i1,Interval i2)
{
return i1.end-i2.end;
}
});
heap.add(intervals[0]);
for(int i =1;i<intervals.length;i++)
{
Interval current = heap.poll();
if(intervals[i].start >= current.end)
{
current.end = intervals[i].end;
}
else
{
heap.add(intervals[i]);
}
heap.add(current);
}
return heap.size();
}
}