Given two arrays start[] and end[] such that start[i] is the starting time of ith meeting and end[i] is the ending time of ith meeting. Task is to find minimum number of rooms required to attend all meetings.
Note: A person can also attend a meeting if it's starting time is same as the previous meeting's ending time.
Examples:
Input: start[] = [1, 10, 7], end[] = [4, 15, 10]
Output: 1
Explanation: Since all the meetings are held at different times, it is possible to attend all the meetings in a single room.Input: start[] = [2, 9, 6], end[] = [4, 12, 10]
Output: 2
Explanation: 1st and 2nd meetings can be attended at one room but for 3rd meeting another room is required.
Table of Content
[Naive Approach] Using Two Nested Loops – O(n^2) Time and O(1) Space
The idea is to go through each meeting's start and end times, comparing them with every other meeting to identify any overlap. In other words, for each meeting, we assess how many other meetings are taking place in the room at the same time and maximum among all these meeting will be our required answer.
// C++ program to find minimum no of rooms using
// nested loops.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to find the minimum number of rooms
// required
int minMeetingRooms(vector<int> &start, vector<int> &end) {
int n = start.size();
// room indicates number of rooms
// needed at a time
int room = 1, res = 1;
// Run a nested for-loop to find the overlap
for (int i = 0; i < n; i++) {
// Initially one room is needed
room = 1;
for (int j = 0; j < n; j++) {
if (i != j)
// Increment room when there is an
// overlap
if (start[i]>= start[j] && end[j]>start[i])
room++;
}
// Update the result
res = max(room, res);
}
return res;
}
int main() {
vector<int>start= {2,9,6};
vector<int>end = {4,12,10};
cout << minMeetingRooms(start,end);
return 0;
}
// Java program to find minimum no of rooms using
// nested loops.
import java.util.*;
class GfG {
// Function to find the minimum number of rooms required
static int minMeetingRooms(int[] start, int[] end) {
int n = start.length;
// room indicates number of rooms needed at a time
int room = 1, res = 1;
// Run a nested for-loop to find the overlap
for (int i = 0; i < n; i++) {
// Initially one room is needed
room = 1;
for (int j = 0; j < n; j++) {
if (i != j) {
// Increment room when there is an
// overlap
if (start[i] >= start[j]
&& end[j] > start[i]) {
room++;
}
}
}
// Update the result
res = Math.max(room, res);
}
return res;
}
public static void main(String[] args) {
int[] start = { 2, 9, 6 };
int[] end = { 4, 12, 10 };
System.out.println(minMeetingRooms(start, end));
}
}
# Python program to find minimum no of rooms using
# nested loops.
# Function to find the minimum number of rooms required
def minMeetingRooms(start, end):
n = len(start)
# room indicates number of rooms needed at a time
room = 1
res = 1
# Run a nested for-loop to find the overlap
for i in range(n):
# Initially one room is needed
room = 1
for j in range(n):
if i != j:
# Increment room when there is an overlap
if start[i] >= start[j] and end[j] > start[i]:
room += 1
# Update the result
res = max(room, res)
return res
if __name__ == "__main__":
start = [2, 9, 6]
end = [4, 12, 10]
print(minMeetingRooms(start, end))
// C# program to find minimum no of rooms using
// nested loops.
using System;
class GfG {
// Function to find the minimum number of rooms required
static int minMeetingRooms(int[] start, int[] end) {
int n = start.Length;
// room indicates number of rooms needed at a time
int room = 1, res = 1;
// Run a nested for-loop to find the overlap
for (int i = 0; i < n; i++) {
// Initially one room is needed
room = 1;
for (int j = 0; j < n; j++) {
if (i != j)
// Increment room when there is an overlap
if (start[i] >= start[j] && end[j] > start[i])
room++;
}
// Update the result
res = Math.Max(room, res);
}
return res;
}
static void Main() {
int[] start = { 2, 9, 6 };
int[] end = { 4, 12, 10 };
Console.WriteLine(minMeetingRooms(start, end));
}
}
// JavaScript program to find minimum no of rooms using
// nested loops.
// Function to find the minimum number of rooms required
function minMeetingRooms(start, end) {
let n = start.length;
// room indicates number of rooms needed at a time
let room = 1, res = 1;
// Run a nested for-loop to find the overlap
for (let i = 0; i < n; i++) {
// Initially one room is needed
room = 1;
for (let j = 0; j < n; j++) {
if (i !== j) {
// Increment room when there is an overlap
if (start[i] >= start[j] && end[j] > start[i]) {
room++;
}
}
}
// Update the result
res = Math.max(room, res);
}
return res;
}
// Driver Code
const start = [2, 9, 6];
const end = [4, 12, 10];
console.log(minMeetingRooms(start, end));
Output
2
[Expected Approach] Using Sorting and Two Pointers – O(n*log(n)) Time and O(1) Space
The Idea is to use sorting and two-pointer technique to reduce the time complexity. First, we sort the start and end time arrays of all meetings. Then, using two pointers, we traverse through both arrays to find minimum rooms required.
In this approach, we aim to determine the number of rooms required at any given point in time. These points correspond to the start and end times of the meetings.
Whenever we encounter the start time of a meeting, we increase the room count. Similarly, when we encounter the end time of a meeting, we decrease the room count. The maximum value of the room count at any point during this process represents the minimum number of rooms needed to host all the meetings.
// C++ program to find minimum no of rooms using
// sorting and two pointers.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Function to find the minimum number of rooms
// required
int minMeetingRooms(vector<int> &start, vector<int> &end) {
int n = start.size();
// no. of rooms at any point of time.
int room = 0;
int res = 0;
// sorting the start and end time of meetings
sort(start.begin(), start.end());
sort(end.begin(), end.end());
// pointing to the current index of start array and end array.
int i = 0, j = 0;
while (i < start.size()) {
// encountered start time of meeting.
if (start[i] < end[j]) {
// increase no of rooms
room++;
i++;
}
// encountered end time of meeting.
else {
// decrease no of rooms.
room--;
j++;
}
// updating final result
res = max(res, room);
}
return res;
}
int main() {
vector<int> start = {2, 9, 6};
vector<int> end = {4, 12, 10};
cout << minMeetingRooms(start, end);
return 0;
}
// Java program to find minimum no of rooms using
// sorting and two pointers.
import java.util.*;
class GfG {
// Function to find the minimum number of rooms required
static int minMeetingRooms(int[] start, int[] end) {
int n = start.length;
// no. of rooms at any point of time
int room = 0;
int res = 0;
// sorting the start and end time of meetings
Arrays.sort(start);
Arrays.sort(end);
// pointing to the current index of the start and end array
int i = 0, j = 0;
while (i < start.length) {
// encountered start time of meeting
if (start[i] < end[j]) {
// increase no. of rooms
room++;
i++;
}
// encountered end time of meeting
else {
// decrease no. of rooms
room--;
j++;
}
// updating final result
res = Math.max(res, room);
}
return res;
}
public static void main(String[] args) {
int[] start = {2, 9, 6};
int[] end = {4, 12, 10};
System.out.println(minMeetingRooms(start, end));
}
}
# python program to find minimum no of rooms using
# sorting and two pointers.
# Function to find the minimum number of rooms required
def minMeetingRooms(start, end):
n = len(start)
# no. of rooms at any point of time
room = 0
res = 0
# sorting the start and end time of meetings
start.sort()
end.sort()
# pointing to the current index of the start and end array
i, j = 0, 0
while i < len(start):
# encountered start time of meeting
if start[i] < end[j]:
# increase no. of rooms
room += 1
i += 1
# encountered end time of meeting
else:
# decrease no. of rooms
room -= 1
j += 1
# updating final result
res = max(res, room)
return res
if __name__ == "__main__":
start = [2, 9, 6]
end = [4, 12, 10]
print(minMeetingRooms(start, end))
// C# program to find minimum no of rooms using
// sorting and two pointers.
using System;
class GfG {
// Function to find the minimum number of rooms required
static int minMeetingRooms(int[] start, int[] end) {
int n = start.Length;
// no. of rooms at any point of time
int room = 0;
int res = 0;
// sorting the start and end time of meetings
Array.Sort(start);
Array.Sort(end);
// pointing to the current index of start and end array
int i = 0, j = 0;
while (i < start.Length) {
// encountered start time of meeting
if (start[i] < end[j]) {
// increase no of rooms
room++;
i++;
}
// encountered end time of meeting
else {
// decrease no of rooms
room--;
j++;
}
// updating final result
res = Math.Max(res, room);
}
return res;
}
static void Main() {
int[] start = { 2, 9, 6 };
int[] end = { 4, 12, 10 };
Console.WriteLine(minMeetingRooms(start, end));
}
}
// Javascript program to find minimum no of rooms using
// sorting and two pointers.
// Function to find the minimum number of rooms required
function minMeetingRooms(start, end) {
let n = start.length;
// no. of rooms at any point of time
let room = 0;
let res = 0;
// sorting the start and end time of meetings
start.sort((a, b) => a - b);
end.sort((a, b) => a - b);
// pointing to the current index of the start and end array
let i = 0, j = 0;
while (i < start.length) {
// encountered start time of meeting
if (start[i] < end[j]) {
// increase no. of rooms
room++;
i++;
}
// encountered end time of meeting
else {
// decrease no. of rooms
room--;
j++;
}
// updating final result
res = Math.max(res, room);
}
return res;
}
// Driver Code
const start = [2, 9, 6];
const end = [4, 12, 10];
console.log(minMeetingRooms(start, end));
Output
2
[Alternate Approach] Using Extra Space for Hashing
The idea is that we will create an array of size equal to maximum end time to track the number of rooms needed at each point of time. Here we created extra space to reduce the time complexity required in sorting.
Step by step Implementation:
- Iterate through the start and end times: for each start time, increment the value at that time in the array (i.e., meetCnt[start time] += 1), and for each departure time, decrement the value at that time (i.e., meetCnt[end time ] -= 1).
- This marks the point of time when rooms are occupied and when they become available.
- After updating the array, compute its cumulative sum to determine the maximum number of rooms required at any point in time.
- The highest value in this cumulative sum represents the minimum number of rooms needed.
// C++ program to find minimum no of rooms using
// extra Space.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Function to find the minimum number of rooms
// required
int minMeetingRooms(vector<int> &start, vector<int> &end) {
int n = start.size();
// Number of rooms at any point of time.
int room = 0;
int res = 0;
// Find the maximum end time
int maxEnd = end[0];
for (int i = 1; i < n; i++)
maxEnd = max(maxEnd, end[i]);
// Create a array to store the count of rooms at each
// point of time
vector<int> meetCnt(maxEnd + 2, 0);
// Increment the count at the start time and decrement
// at the end time
for (int i = 0; i < n; i++) {
meetCnt[start[i]]++;
meetCnt[end[i]]--;
}
// Iterate over the vector and keep track of the maximum
// room at any point of time.
for (int i = 0; i <= maxEnd + 1; i++) {
room += meetCnt[i];
res = max(res, room);
}
return res;
}
int main() {
vector<int> start = {2, 9, 6};
vector<int> end = {4, 12, 10};
cout << minMeetingRooms(start, end);
return 0;
}
// Java program to find minimum no of rooms using Extra
// Space
import java.util.*;
class GfG {
// Function to find the minimum number of rooms required
static int minMeetingRooms(int[] start, int[] end) {
int n = start.length;
// no. of rooms at any point of time
int room = 0;
int res = 0;
// Find the maximum end time
int maxEnd = end[0];
for (int i = 1; i < n; i++)
maxEnd = Math.max(maxEnd, end[i]);
// Create an array to store the count of rooms at
// each point of time
int[] meetCnt = new int[maxEnd + 2];
// Increment the count at the start time and
// decrement at the end time
for (int i = 0; i < n; i++) {
meetCnt[start[i]]++;
meetCnt[end[i]]--;
}
// Iterate over the array and keep track of the
// maximum room at any point of time
for (int i = 0; i <= maxEnd + 1; i++) {
room += meetCnt[i];
res = Math.max(res, room);
}
return res;
}
public static void main(String[] args) {
int[] start = { 2, 9, 6 };
int[] end = { 4, 12, 10 };
System.out.println(minMeetingRooms(start, end));
}
}
# Python program to find minimum no of rooms using Extra Space
# Function to find the minimum number of rooms required
def minMeetingRooms(start, end):
n = len(start)
# no. of rooms at any point of time
room = 0
res = 0
# Find the maximum end time
maxEnd = end[0]
for i in range(1, n):
maxEnd = max(maxEnd, end[i])
# Create a list to store the count of rooms at each point of time
meetCnt = [0] * (maxEnd + 2)
# Increment the count at the start time and decrement at the end time
for i in range(n):
meetCnt[start[i]] += 1
meetCnt[end[i]] -= 1
# Iterate over the list and keep track of the maximum room at any point of time
for i in range(maxEnd + 2):
room += meetCnt[i]
res = max(res, room)
return res
if __name__ == "__main__":
start = [2, 9, 6]
end = [4, 12, 10]
print(minMeetingRooms(start, end))
// C# program to find minimum no of rooms using Extra Space
using System;
class GfG {
// Function to find the minimum number of rooms required
static int minMeetingRooms(int[] start, int[] end) {
int n = start.Length;
// no. of rooms at any point of time
int room = 0;
int res = 0;
// Find the maximum end time
int maxEnd = end[0];
for (int i = 1; i < n; i++)
maxEnd = Math.Max(maxEnd, end[i]);
// Create an array to store the count of rooms at each point of time
int[] meetCnt = new int[maxEnd + 2];
// Increment the count at the start time and decrement at the end time
for (int i = 0; i < n; i++) {
meetCnt[start[i]]++;
meetCnt[end[i]]--;
}
// Iterate over the array and keep track of the maximum rooms at any point of time
for (int i = 0; i <= maxEnd + 1; i++) {
room += meetCnt[i];
res = Math.Max(res, room);
}
return res;
}
static void Main() {
int[] start = { 2, 9, 6 };
int[] end = { 4, 12, 10 };
Console.WriteLine(minMeetingRooms(start, end));
}
}
// JavaScript program to find minimum no of rooms using Extra Space
// Function to find the minimum number of rooms required
function minMeetingRooms(start, end) {
let n = start.length;
// no. of rooms at any point of time
let room = 0;
let res = 0;
// Find the maximum end time
let maxEnd = end[0];
for (let i = 1; i < n; i++)
maxEnd = Math.max(maxEnd, end[i]);
// Create an array to store the count of rooms at each point of time
let meetCnt = new Array(maxEnd + 2).fill(0);
// Increment the count at the start time and decrement at the end time
for (let i = 0; i < n; i++) {
meetCnt[start[i]]++;
meetCnt[end[i]]--;
}
// Iterate over the array and keep track of the maximum room at any point of time
for (let i = 0; i <= maxEnd + 1; i++) {
room += meetCnt[i];
res = Math.max(res, room);
}
return res;
}
// Driver code
let start = [2, 9, 6];
let end = [4, 12, 10];
console.log(minMeetingRooms(start, end));
Output
2
Time Complexity: O(maxEnd + n), maxEnd represents the maximum value of end array.
Auxiliary Space: O(maxEnd)