Topic:
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
.
Links: http://leetcode.com/problems/meeting-rooms-ii/
Exercises
Given a interval array, ask for the minimum number of classrooms required. The initial idea is to scan the line algorithm sweeping-line algorithm, sort the array first, then maintain a min-oriented heap. Iterate through the sorted array, add interval[i].end to the heap each time, and compare Interval.start with Pq.peek (), if Interval[i].start >= Pq.peek (), stating Pq.peek () The meeting represented is over, and we can remove the meeting's end time from the heap and continue to compare the next Pq.peek (). We tried to update maxoverlappingmeetings after the comparison was complete. Like scan line algorithm and heap also need to review, straight line, matrix intersect also can use scan line algorithm.
Time Complexity-o (NLOGN), Space complexity-o (n)
/*** Definition for a interval. * public class Interval {* int start; * int end; * Interval () {start = 0; end = 0; } * Interval (int s, int e) {start = s; end = e;} }*/ Public classSolution { Public intminmeetingrooms (interval[] intervals) {if(Intervals = =NULL|| Intervals.length = = 0) return0; Arrays.sort (intervals,NewComparator<interval>() { Public intCompare (Interval t1, Interval T2) {if(T1.start! =T2.start)returnT1.start-T2.start; Else returnT1.end-T2.end; } }); intmaxoverlappingmeetings = 0; Priorityqueue<Integer> PQ =NewPriorityqueue<> ();//min oriented priority Queue for(inti = 0; i < intervals.length; i++) {//Sweeping-line AlgorithmsPq.add (intervals[i].end); while(Pq.size () > 0 && intervals[i].start >=Pq.peek ()) Pq.remove (); Maxoverlappingmeetings=Math.max (Maxoverlappingmeetings, Pq.size ()); } returnmaxoverlappingmeetings; }}
Reference:
253. Meeting Rooms II