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.
For example,
Given [[0, 30],[5, 10],[15, 20]]
,
return 2
.
思路是将数组根据start排序,然后再维护一个end的小根堆,每次取堆顶元素,遍历数组,若堆顶end小于等于当前start,则说明可以将这次会议放到此会议室,将堆顶元素弹出即可,代码:
public int minMeetingRooms(Interval[] intervals) { int rooms=0; Arrays.sort(intervals, new Comparator<Interval>() { @Override public int compare(Interval o1, Interval o2) { return o1.start-o2.start; } }); Queue<Interval> queue=new PriorityQueue<>(new Comparator<Interval>() { @Override public int compare(Interval o1, Interval o2) { return o1.end-o2.end; } }); for(int i=0;i<intervals.length;i++){ if(!queue.isEmpty()&&queue.peek().end<=intervals[i].start){ queue.poll(); } queue.offer(intervals[i]); rooms=Math.max(rooms,queue.size()); } return rooms; }