253. Meeting Rooms II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.