Minimum Number of Meeting Room Required Problem in JavaIt is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Adobe, Apple, Infosys, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problemsolving skill of the interviewee. So, in this section, we are going to find minimum number of meeting rooms required problem in Java with different approaches and logic. Also, we will create Java programs for the same. Problem StatementIn this problem, we have given an array (start time and end time) of meeting time intervals. We have to find the minimum number of meeting rooms required. ExampleSuppose, an array of meeting start and end time is given { { 1, 20 }, { 19, 30}, { 40, 70 } }. Let's assign a room to each interval. Assign room 1 to first interval (1, 20). The second interval start when the first interval is going on, so assign room 2 to the second interval (19, 30). The third interval starts after the second interval. Therefore, assign room 2 to the third interval (40, 70). Hence, we required total two rooms for meeting. Let's see another example. First, we will use a priority queue or chronological order for the array to tackle over the problem. { { 1, 18 }, { 18, 23 }, { 15, 29 }, {4, 15}, {2, 11}, {5, 13} } Create two arrays for storing the interval (meeting start and end time). Start Time = {1, 18, 15, 4, 2, 5} End Time = {18, 23, 29, 15, 11, 13} For better understanding, we will create a table of time intervals.
Now, we will iterate over both the arrays. Check, whether the element in the start array is smaller than the element in the end array. If the condition returns true, create a new room and assign that time interval. After that proceed to the next element in the start time array. On the other hand, if the end element was smaller than the start element, we search for an existing meeting room. Once that is done, we move the pointer in both rows by one. Therefore, Meeting one can be done in meeting room 1 from 1  18. Meeting five can be done in meeting room 2 from 2  11. Meeting four can be done in meeting room 3 from 4  15. Meeting six can be done in meeting room 4 from 5  13. Meeting three can be done in meeting room 2 from 15  29 because it is free in this interval. Meeting two can be done in meeting room 4 from 18  23 because it is free in this interval. Solution to the ProblemFirst, create two arrays in sorted order. First array denotes the meeting start time and the second array denotes the meeting end time. Define two variable that iterate over the start and end array say i and j, respectively. Define another variable named curr that will count the current number of meetings going on.
Now our answer is the highest value of the variable curr throughout the process. Java Program to Find the Minimum number of Meeting Room RequiredMeetingRoom1.java Output: Total Number of Meeting room required is: 4 Let's see another approach for the same problem. We sort the time intervals based on the start time first, have a counter, compare each start time and the corresponding end time by adding end times to a priority queue. If start time is lesser, increment the counter, else pop the end time from queue. Let's implement the approach in a Java program. MeetingRoom2.java Output: Minimum number of meeting rooms required is 2 ComplexityThe time complexity of the above approach is O(nlogn) because of sorting arrays. The space complexity of the above approach is O(n).
Next TopicMove Zeros to End in Java
