# Minimum Number of Meeting Room Required Problem in Java

It 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 problem-solving 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 Statement

In 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.

### Example

Suppose, 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.

 Start Time 1 18 15 4 2 5 End Time 18 23 29 15 11 13

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 Problem

First, 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.

• If the condition start[i] < end[j] becomes true, it means that the j-th meeting is going on and we require another meeting room for the i-th meeting. Increment the variable curr and i by one, because we have started the i-th meeting.
• If the condition start[i] >= end[j] becomes true, it means that the j-th meeting is over and that room is free now, decrement the variable curr by one and increment the variable j because the j-th meeting has ended.

Now our answer is the highest value of the variable curr throughout the process.

### Java Program to Find the Minimum number of Meeting Room Required

MeetingRoom1.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
```

## Complexity

The time complexity of the above approach is O(nlogn) because of sorting arrays. The space complexity of the above approach is O(n).