[Leetcode] Meeting Rooms II Conference Room two

Source: Internet
Author: User

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 .

This question is the expansion of the previous meeting rooms, the question only let us participate in all the meetings, that is to see there is no time conflict between the Conference, and this problem let us ask for at least a few meeting rooms, sometimes the conflict must be arranged in different conference rooms. There are several solutions to this problem, we first look at the use of map to do, we traverse the time interval, for the start time, the mapping value from 1, for the end time, the map value is reduced by 1, and then we define the result variable res, and the number of rooms rooms, we traverse the map, the time from small to large, the number of , then updates the result res, encounters a start time, the map is positive, the number of rooms increases, if one time is the end time of a meeting, and also the start time of another meeting, the map value is reduced to 0, and no new room is allocated, and the end time mapping value is negative and no increase in the number of rooms. Using this idea we can write the code as follows:

Solution One:

classSolution { Public:    intMinmeetingrooms (vector<interval>&intervals) {Map<int,int>m;  for(Auto a:intervals) {++M[a.start]; --M[a.end]; }        intRooms =0, res =0;  for(Auto it:m) {res= Max (res, rooms + =It.second); }        returnRes; }};

The second method is to use two one-dimensional array to do, save the start time and end time, and then each order, we define the result variable res and the end time pointer Endpos, and then we start to traverse, if the current start time is less than the end time pointer time, the result is increased by 1, and the end time pointer increment 1 , so that we can find overlapping time periods to arrange new rooms, see the code below:

Solution Two:

classSolution { Public:    intMinmeetingrooms (vector<interval>&intervals) {Vector<int>starts, ends; intres =0, Endpos =0;  for(Auto a:intervals) {starts.push_back (A.start);        Ends.push_back (A.end);        } sort (Starts.begin (), Starts.end ());        Sort (Ends.begin (), Ends.end ());  for(inti =0; I < intervals.size (); ++i) {if(Starts[i] < Ends[endpos]) + +Res; Else++Endpos; }        returnRes; }};

Let's take a look at a method that uses the least heap to solve a problem, this method first sorts all the time intervals according to the starting time, then creates a new minimum heap, starts to traverse the time interval, if the heap is not empty, and the first element is less than equal to the starting time of the current interval, we remove the first element in the heap, and Since the minimum heap is small in front, then if the first element is less than or equal to the start time, the previous meeting has ended, you can start the next meeting with the conference room, so you don't have to allocate a new conference room, the number of elements in the heap after the completion of the traversal is the number of meeting rooms required, see code below;

Solution Three:

classSolution { Public:    intMinmeetingrooms (vector<interval>&intervals) {Sort (Intervals.begin (), Intervals.end (), [] (ConstInterval &a,ConstInterval &b) {returnA.start <B.start;}); Priority_queue<int, vector<int, greater<int>>Q;  for(Auto a:intervals) {if(!q.empty () && q.top () <=A.start) Q.pop ();        Q.push (A.end); }        returnq.size (); }};

Similar topics:

Meeting Rooms

Resources:

Https://leetcode.com/discuss/50948/c-o-n-log-n-584-ms-3-solutions

Https://leetcode.com/discuss/71846/super-easy-java-solution-beats-98-8%25

Https://leetcode.com/discuss/64686/concise-c-solution-with-min_heap-sort-greedy

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Meeting Rooms II Conference Room two

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.