/** 253.Meeting Rooms II * 2016-6-20 by Mingyang * greedy algorithm, and rearrange array to certain distance like */ Public intminmeetingrooms (interval[] intervals) {if(Intervals = =NULL|| Intervals.length = = 0)return0; Arrays.sort (intervals,NewComparator<interval>(){ Public intCompare (Interval i1, Interval i2) {returnI1.start-I2.start; } }); //use heaps to manage the end time of a roomPriorityqueue<integer> endtimes =NewPriorityqueue<integer>(); Endtimes.offer (intervals[0].end); for(inti = 1; i < intervals.length; i++){ //If the start time of the current time period is greater than the earliest ending time, you can update the earliest end time to the end of the current time period and, if less, add a new end time that represents the new room if(Intervals[i].start >=Endtimes.peek ()) {Endtimes.poll (); } endtimes.offer (Intervals[i].end); } //How many rooms are there at the end of the day ? returnendtimes.size (); }
253.Meeting Rooms II