[leetcode#253] Meeting Rooms II

Source: Internet
Author: User

problem:

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 .

Analysis:

This problem likes skyline problem very much, although we could the say the problem are much easy. The idea was to use a kind of greedy problem. Basic Idea:we use a priority queue to record each class' s end time, the earliest available the ' s end time is at the minimum heap ' s top. Now we have aNewinterval (meeting).1. If The meeting's start time after the Earilest "s end time. It means we could actually arrange theNewMeeting into ThisWe have no need to open aNew.---------------------------------------------------------------if(Intervals[i].start >=Min_heap.peek ())    {Min_heap.poll (); Min_heap.offer (intervals[i].end);} The reason why we must append theNewMeeting after the Earilist AvaialbeifThere is also other rooms available at that time?Suppose we have both meeting and aNewMeeting . And A is the earilist available. A [] new_1[]b [] new_1[]wheather we add theNewMeeting into class A or B, it actually would result in the sameNewEnd time forthat. And we know all other meetings must happen after theNewMeeting. Suppose we have aNewMeeting called "New_2". IFF New_1 was added into the---[] new_1[]b [] new_2[]iff New_2 was ad Ded into the hostel BA [] new_2[]b [] new_1[]as You can see from the change!!!If We wipe out the name of each of the, it actually result in same available time structure among rooms.2. If The meeting ' s start time before the Earilest, the end time. It means ThisMeeting actually conflict with all other class ' s meeting and we have to open a new class.-------------------------------------- -------------------------if(Intervals[i].start <Min_heap.peek ())    {Min_heap.offer (intervals[i].end); Count++;}---------------------------------------------------------------

Solution:

 Public classSolution { Public intminmeetingrooms (interval[] intervals) {if(Intervals = =NULL)            Throw NewIllegalArgumentException ("Intervals is null"); intLen =intervals.length; if(Len <= 1)            returnLen; Arrays.sort (intervals,NewComparator<interval>() {@Override Public intCompare (Interval A, Interval b) {if(A.start = =B.start)returnA.end-B.end; returnA.start-B.start;            }        }); Priorityqueue<Integer> min_heap =NewPriorityqueue<integer> (10); Min_heap.offer (intervals[0].end); intCount = 1;  for(inti = 1; i < Len; i++) {            if(Intervals[i].start <Min_heap.peek ())                {Min_heap.offer (intervals[i].end); Count++; } Else{min_heap.poll ();            Min_heap.offer (Intervals[i].end); }        }        returncount; }}

[leetcode#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.