Meeting Rooms - Room with Maximum Meetings
Last Updated : 28 Apr, 2025
Given an integer n representing the number of rooms numbered from 0 to n - 1 and a 2D integer array meetings[][] where meetings[i] = [starti, endi] indicates that a meeting is scheduled during the half-closed time interval [starti, endi]. The task is to find the room number that hosts the most meetings. If multiple rooms have the same highest number of meetings, find the smallest room number among them.
Meeting Allocation Rules
- When a meeting starts, assign it to the available room with the smallest number.
- If no rooms are free, delay the meeting until the earliest room becomes available. The delayed meeting retains its original duration.
- When a room becomes free, assign it to the delayed meeting with the earliest original start timing.
Note: A person can also attend a meeting if it's starting time is same as the previous meeting's ending time.
Examples:
Input: n=2, meetings[][]=[[0, 6], [2, 3], [3, 7], [4, 8], [6, 8]]
Output: 1
Explanations:
Time 0: Both rooms available. [0,6] starts in room 0.
Time 2: Room 0 busy until 6. Room 1 available. [2,3] starts in room 1.
Time 3: Room 1 frees up. [3,7] starts in room 1.
Time 4: Both rooms busy. [4,8] is delayed.
Time 6: Room 0 frees up. Delayed [4,8] starts in room 0 [6,10).
Time 6: [6,8] arrives but both rooms busy. It’s delayed.
Time 7: Room 1 frees up. Delayed [6,8] starts in room 1 [7,9).
Room 1 hosted 3 meetings which is maximum.
Input: n = 4, meetings[][] = [[0, 8], [1, 4], [3, 4], [2, 3]
Output: 2
Explanation:
Time 0: All rooms available. [0,8] starts in room 0.
Time 1: Room 0 busy until 8. Rooms 1, 2, 3 available. [1,4] starts in room 1.
Time 2: Rooms 0 and 1 busy. Rooms 2, 3 available. [2,3] starts in room 2.
Time 3: Room 2 frees up. [3,4] starts in room 2.
Room 2 hosted 2 meetings which is maximum.
[Naive Approach] Using Sorting and Two arrays - O(n*m) Time and O(n) Space
The idea is to use two array: first array (avail)
which stores
the ending time of ongoing meeting in each room and second array (freq) to store the meeting count of each room.
Step by step Implementation:
- First, we will sort the meeting array, as we need to schedule the meeting with the earliest starting time first.
- Then, we will iterate over each meeting and schedule it in one of the available rooms with the smallest room number.
- Increase the count of meetings for that room, which we are storing in the f
req
array.
- In case if no room is available then we will assign that meeting to room which will be available in near time.
- Finally we will iterate over freq array to know the room which hosts the maximum meeting.
C++ // C++ program to find the room that hosts the highest number of // meetings Using Sorting and Two arrays #include <iostream> #include <vector> #include <algorithm> using namespace std; // Function to calculate the room that hosts the maximum meetings. int mostBooked(int n, vector<vector<int>> &meetings) { vector<int> avail(n, 0); vector<int> freq(n, 0); // Sorting the meetings vector so that the // meeting schedule is in order of start time. sort(meetings.begin(), meetings.end()); for (int i = 0; i < meetings.size(); i++) { int room = -1; // Checking if any room is free or not. for (int j = 0; j < n; j++) { if (avail[j] <= meetings[i][0]) { room = j; break; } } // Updating the room available time and // room's meeting count, if we get an available room. if (room != -1) { avail[room] = meetings[i][1]; freq[room]++; continue; } int k = 1e9; // If no room is available, checking for a room whose // available time is nearest to the start time of the meeting. for (int j = 0; j < n; j++) { if (k > avail[j]) { k = avail[j]; room = j; } } avail[room] = k + meetings[i][1] - meetings[i][0]; freq[room]++; } int maxfreq = 0; int res = 0; // Finding the room that hosts the maximum meetings. for (int i = 0; i < n; i++) { if (maxfreq < freq[i]) { maxfreq = freq[i]; res = i; } } return res; } int main() { int n = 2; vector<vector<int>> meetings = {{0, 6}, {2, 3}, {3, 7}, {4, 8}, {6, 8}}; cout << mostBooked(n, meetings); }
Java // Java program to find the room that hosts the highest number of // meetings Using Sorting and Two arrays import java.util.*; class GfG { // Function to calculate the room that hosts the maximum meetings. static int mostBooked(int n, int[][] meetings) { int[] avail = new int[n]; int[] freq = new int[n]; // Sorting the meetings vector so that the // meeting schedule is in order of start time. Arrays.sort(meetings, (a, b) -> a[0] - b[0]); for (int i = 0; i < meetings.length; i++) { int room = -1; // Checking if any room is free or not. for (int j = 0; j < n; j++) { if (avail[j] <= meetings[i][0]) { room = j; break; } } // Updating the room available time and // room's meeting count, if we get an available room. if (room != -1) { avail[room] = meetings[i][1]; freq[room]++; continue; } int k = (int)1e9; // If no room is available, checking for a room whose // available time is nearest to the start time of the meeting. for (int j = 0; j < n; j++) { if (k > avail[j]) { k = avail[j]; room = j; } } avail[room] = k + meetings[i][1] - meetings[i][0]; freq[room]++; } int maxfreq = 0; int res = 0; // Finding the room that hosts the maximum meetings. for (int i = 0; i < n; i++) { if (maxfreq < freq[i]) { maxfreq = freq[i]; res = i; } } return res; } public static void main(String[] args) { int n = 2; int[][] meetings = {{0, 6}, {2, 3}, {3, 7}, {4, 8}, {6, 8}}; System.out.println(mostBooked(n, meetings)); } }
Python # Python program to find the room that hosts the highest number of # meetings Using Sorting and Two arrays # Function to calculate the room that hosts the maximum meetings. def mostBooked(n, meetings): avail = [0] * n freq = [0] * n # Sorting the meetings vector so that the # meeting schedule is in order of start time. meetings.sort() for i in range(len(meetings)): room = -1 # Checking if any room is free or not. for j in range(n): if avail[j] <= meetings[i][0]: room = j break # Updating the room available time and # room's meeting count, if we get an available room. if room != -1: avail[room] = meetings[i][1] freq[room] += 1 continue k = int(1e9) # If no room is available, checking for a room whose # available time is nearest to the start time of the meeting. for j in range(n): if k > avail[j]: k = avail[j] room = j avail[room] = k + meetings[i][1] - meetings[i][0] freq[room] += 1 maxfreq = 0 res = 0 # Finding the room that hosts the maximum meetings. for i in range(n): if maxfreq < freq[i]: maxfreq = freq[i] res = i return res if __name__ == "__main__": n = 2 meetings = [[0, 6], [2, 3], [3, 7], [4, 8], [6, 8]] print(mostBooked(n, meetings))
C# // C# program to find the room that hosts the highest number of // meetings Using Sorting and Two arrays using System; class GfG { // Function to calculate the room that hosts the maximum meetings. static int mostBooked(int n, int[][] meetings) { int[] avail = new int[n]; int[] freq = new int[n]; // Sorting the meetings vector so that the // meeting schedule is in order of start time. Array.Sort(meetings, (a, b) => a[0].CompareTo(b[0])); for (int i = 0; i < meetings.Length; i++) { int room = -1; // Checking if any room is free or not. for (int j = 0; j < n; j++) { if (avail[j] <= meetings[i][0]) { room = j; break; } } // Updating the room available time and // room's meeting count, if we get an available room. if (room != -1) { avail[room] = meetings[i][1]; freq[room]++; continue; } int k = (int)1e9; // If no room is available, checking for a room whose // available time is nearest to the start time of the meeting. for (int j = 0; j < n; j++) { if (k > avail[j]) { k = avail[j]; room = j; } } avail[room] = k + meetings[i][1] - meetings[i][0]; freq[room]++; } int maxfreq = 0; int res = 0; // Finding the room that hosts the maximum meetings. for (int i = 0; i < n; i++) { if (maxfreq < freq[i]) { maxfreq = freq[i]; res = i; } } return res; } static void Main(string[] args) { int n = 2; int[][] meetings = new int[][] { new int[] {0, 6}, new int[] {2, 3}, new int[] {3, 7}, new int[] {4, 8}, new int[] {6, 8} }; Console.WriteLine(mostBooked(n, meetings)); } }
JavaScript // JavaScript program to find the room that hosts the highest number of // meetings Using Sorting and Two arrays // Function to calculate the room that hosts the maximum meetings. function mostBooked(n, meetings) { let avail = new Array(n).fill(0); let freq = new Array(n).fill(0); // Sorting the meetings vector so that the // meeting schedule is in order of start time. meetings.sort((a, b) => a[0] - b[0]); for (let i = 0; i < meetings.length; i++) { let room = -1; // Checking if any room is free or not. for (let j = 0; j < n; j++) { if (avail[j] <= meetings[i][0]) { room = j; break; } } // Updating the room available time and // room's meeting count, if we get an available room. if (room !== -1) { avail[room] = meetings[i][1]; freq[room]++; continue; } let k = 1e9; // If no room is available, checking for a room whose // available time is nearest to the start time of the meeting. for (let j = 0; j < n; j++) { if (k > avail[j]) { k = avail[j]; room = j; } } avail[room] = k + meetings[i][1] - meetings[i][0]; freq[room]++; } let maxfreq = 0; let res = 0; // Finding the room that hosts the maximum meetings. for (let i = 0; i < n; i++) { if (maxfreq < freq[i]) { maxfreq = freq[i]; res = i; } } return res; } let n = 2; let meetings = [[0, 6], [2, 3], [3, 7], [4, 8], [6, 8]]; console.log(mostBooked(n, meetings));
[Expected Approach] Using Two priority queue - O(m*log m+m*log n) Time and O(n) Space
The above approach can be optimized by using two priority queues: one to find the available room with the smallest number and the other to track ongoing meetings.
Step by step Implementation:
- First, we sort the meeting array, as we need to schedule the meetings with the earliest starting times first.
- We place all the rooms into the
avail
priority queue because all rooms are available before scheduling any meeting. - Next, we iterate over each meeting. For each meeting, we pop elements (availability time, room number) from the
occ
queue if the room becomes available before the start time of the current meeting, and we push that room number into the avail
queue. - We then schedule the meeting in the room represented by the top element (room number) of the
avail
queue. - If no room is available, we assign the meeting to the top element of the
occ
queue, as that room will be free at the earliest time. - Finally, we iterate over the
cnt
array to find the room that hosts the maximum number of meetings.
C++ // C++ program to find the room that hosts the highest number of // meetings Using Priority Queue #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; // Function to calculate the room that hosts the maximum meetings. int mostBooked(int n, vector<vector<int>> &meetings) { // Count of meetings per room vector<int> cnt(n, 0); // Min-heap for occupied rooms: (end time, room number) priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> occ; // Min-heap for available rooms: room numbers priority_queue<int, vector<int>, greater<int>> avail; // Initialize all rooms as available for (int i = 0; i < n; ++i) avail.push(i); // Sort meetings by start time sort(meetings.begin(), meetings.end()); for (auto &m : meetings) { int s = m[0], e = m[1]; // Release rooms that have become available by time s while (!occ.empty() && occ.top().first <= s) { avail.push(occ.top().second); occ.pop(); } if (!avail.empty()) { // Assign to the smallest available room int room = avail.top(); avail.pop(); occ.push({e, room}); cnt[room]++; } else { // All rooms are occupied; assign to the room that becomes free earliest int endTime = occ.top().first; int room = occ.top().second; occ.pop(); occ.push({endTime + (e - s), room}); cnt[room]++; } } // Find the room with the maximum number of meetings int maxCnt = 0, res = 0; for (int i = 0; i < n; ++i) { if (cnt[i] > maxCnt) { maxCnt = cnt[i]; res = i; } } return res; } int main() { int n = 2; vector<vector<int>> meetings = {{0, 6}, {2, 3}, {3, 7}, {4, 8}, {6, 8}}; cout << mostBooked(n, meetings); }
Java // Java program to find the room that hosts the highest number of // meetings Using Priority Queue import java.util.*; class GfG { static int mostBooked(int n, int[][] meetings) { // Count of meetings per room int[] cnt = new int[n]; // PriorityQueue for occupied rooms: (end time, room number) PriorityQueue<int[]> occ = new PriorityQueue<>(new Comparator<int[]>() { public int compare(int[] a, int[] b) { if (a[0] != b[0]) { // Compare by end time return Integer.compare(a[0], b[0]); } // If end times are equal, compare by room number return Integer.compare( a[1], b[1]); } }); // PriorityQueue for available rooms: room numbers PriorityQueue<Integer> avail = new PriorityQueue<>(); for (int i = 0; i < n; i++) { avail.offer(i); } // Sort meetings by start time, then by end time // if start times are equal Arrays.sort(meetings, new Comparator<int[]>() { public int compare(int[] a, int[] b) { if (a[0] != b[0]) { return Integer.compare(a[0], b[0]); } return Integer.compare(a[1], b[1]); } }); for (int[] m : meetings) { int s = m[0]; // Start time int e = m[1]; // End time // Release all rooms that have become available // by time s while (!occ.isEmpty() && occ.peek()[0] <= s) { avail.offer(occ.poll()[1]); } if (!avail.isEmpty()) { // Assign to the smallest available room int r = avail.poll(); occ.offer(new int[] {e, r}); cnt[r]++; } else { // All rooms are occupied; assign to the room // that becomes free earliest int[] earliest = occ.poll(); int t = earliest[0]; int r = earliest[1]; occ.offer(new int[] {t + (e - s), r}); cnt[r]++; } } // Find the room with the maximum number of meetings int maxCnt = 0; int res = 0; for (int i = 0; i < n; i++) { if (cnt[i] > maxCnt) { maxCnt = cnt[i]; res = i; } } return res; } public static void main(String[] args) { int n = 2; int[][] meetings = { { 0, 6 }, { 2, 3 }, { 3, 7 }, { 4, 8 }, { 6, 8 } }; System.out.println(mostBooked(n, meetings)); } }
Python # Pyhton program to find the room that hosts the highest number of # meetings Using Priority Queue import heapq # Function to calculate the room that hosts the maximum meetings. def mostBooked(n, meetings): # Count of meetings per room cnt = [0] * n # Min-heap for occupied rooms: (end time, room number) occ = [] # Min-heap for available rooms: room numbers avail = list(range(n)) # Sort meetings by start time meetings.sort() for s, e in meetings: # Release rooms that have become available by time s while occ and occ[0][0] <= s: _, r = heapq.heappop(occ) heapq.heappush(avail, r) if avail: # Assign to the smallest available room r = heapq.heappop(avail) heapq.heappush(occ, (e, r)) cnt[r] += 1 else: # All rooms are occupied; assign to the room that becomes free earliest t, r = heapq.heappop(occ) heapq.heappush(occ, (t + (e - s), r)) cnt[r] += 1 # Find the room with the maximum number of meetings res = max(range(n), key=lambda i: cnt[i]) return res if __name__ == "__main__": n = 2 meetings = [[0, 6], [2, 3], [3, 7], [4, 8], [6, 8]] print(mostBooked(n, meetings))
C# // C# program to find the room that hosts the highest number of // meetings Using Priority Queue using System; using System.Collections.Generic; class GfG { // Function to calculate the room that hosts the maximum meetings. static int mostBooked(int n, int[][] meetings) { // Count of meetings per room int[] cnt = new int[n]; // Min-heap for occupied rooms: (end time, room number) PriorityQueue<Pair> occ = new PriorityQueue<Pair>(new ComparerOcc()); // Min-heap for available rooms: room numbers PriorityQueue<int> avail = new PriorityQueue<int>(new ComparerAvail()); // Initialize all rooms as available for (int i = 0; i < n; ++i) avail.Enqueue(i); // Sort meetings by start time Array.Sort(meetings, (a, b) => a[0].CompareTo(b[0])); foreach (var m in meetings) { int s = m[0], e = m[1]; // Release rooms that have become available by time s while (occ.Count > 0 && occ.Peek().first <= s) { avail.Enqueue(occ.Peek().second); occ.Dequeue(); } if (avail.Count > 0) { // Assign to the smallest available room int room = avail.Dequeue(); occ.Enqueue(new Pair(e, room)); cnt[room]++; } else { // All rooms are occupied; assign to the // room that becomes free earliest int endTime = occ.Peek().first; int room = occ.Peek().second; occ.Dequeue(); occ.Enqueue(new Pair(endTime + (e - s), room)); cnt[room]++; } } // Find the room with the maximum number of meetings int maxCnt = 0, res = 0; for (int i = 0; i < n; ++i) { if (cnt[i] > maxCnt) { maxCnt = cnt[i]; res = i; } } return res; } static void Main(string[] args) { int n = 2; int[][] meetings = new int[][] { new int[] {0, 6}, new int[] {2, 3}, new int[] {3, 7}, new int[] {4, 8}, new int[] {6, 8} }; Console.WriteLine(mostBooked(n, meetings)); } } class Pair { public int first; public int second; public Pair(int first, int second) { this.first = first; this.second = second; } } class ComparerOcc : IComparer<Pair> { public int Compare(Pair a, Pair b) { if (a.first != b.first) return a.first - b.first; return a.second - b.second; } } class ComparerAvail : IComparer<int> { public int Compare(int a, int b) { return a - b; } } // Custom Priority queue class PriorityQueue<T> { private List<T> heap; private IComparer<T> comparer; public PriorityQueue(IComparer<T> comparer = null) { this.heap = new List<T>(); this.comparer = comparer ?? Comparer<T>.Default; } public int Count => heap.Count; public void Enqueue(T item) { heap.Add(item); int i = heap.Count - 1; while (i > 0) { int parent = (i - 1) / 2; if (comparer.Compare(heap[parent], heap[i]) <= 0) break; Swap(parent, i); i = parent; } } public T Dequeue() { if (heap.Count == 0) throw new InvalidOperationException("Priority queue is empty."); T result = heap[0]; int last = heap.Count - 1; heap[0] = heap[last]; heap.RemoveAt(last); last--; int i = 0; while (true) { int left = 2 * i + 1; if (left > last) break; int right = left + 1; int minChild = left; if (right <= last && comparer.Compare(heap[right], heap[left]) < 0) minChild = right; if (comparer.Compare(heap[i], minChild >= 0 ? heap[minChild] : default(T)) <= 0) break; Swap(i, minChild); i = minChild; } return result; } public T Peek() { if (heap.Count == 0) throw new InvalidOperationException("Priority queue is empty."); return heap[0]; } private void Swap(int i, int j) { T temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; } }
JavaScript //Driver Code Starts // JavaScript program to find the room that hosts the highest number of // meetings Using Priority Queue // Creating PriorityQueue class to implement priority queue class PriorityQueue { constructor(comparator = (a, b) => a > b) { this.heap = []; this.comparator = comparator; } size() { return this.heap.length; } peek() { return this.heap[0]; } push(value) { this.heap.push(value); this._heapifyUp(); } pop() { const top = this.peek(); const bottom = this.heap.pop(); if (this.heap.length > 0) { this.heap[0] = bottom; this._heapifyDown(); } return top; } _heapifyUp() { let index = this.heap.length - 1; while (index > 0) { let parent = Math.floor((index - 1) / 2); if (this.comparator(this.heap[index], this.heap[parent])) { [this.heap[index], this.heap[parent]] = [ this.heap[parent], this.heap[index] ]; index = parent; } else { break; } } } _heapifyDown() { let index = 0; const length = this.heap.length; while (true) { let left = 2 * index + 1; let right = 2 * index + 2; let target = index; if (left < length && this.comparator(this.heap[left], this.heap[target])) { target = left; } if (right < length && this.comparator(this.heap[right], this.heap[target])) { target = right; } if (target !== index) { [this.heap[index], this.heap[target]] = [ this.heap[target], this.heap[index] ]; index = target; } else { break; } } } } //Driver Code Ends // function to find room which host maximum meeting. function mostBooked(n, meetings) { const cnt = new Array(n).fill(0); // Priority Queue for occupied rooms: [end time, room // number] const occ = new PriorityQueue((a, b) => { if (a[0] !== b[0]) { return a[0] < b[0]; // Earlier end time first } return a[1] < b[1]; // If end times are equal, // smaller room number first }); // Priority Queue for available rooms: room numbers const avail = new PriorityQueue( (a, b) => a < b); // Smaller room number first for (let i = 0; i < n; i++) { avail.push(i); } // Sort meetings by start time, then by end time meetings.sort((a, b) => { if (a[0] !== b[0]) { return a[0] - b[0]; } return a[1] - b[1]; }); for (const m of meetings) { let s = m[0]; let e = m[1]; // Release all rooms that have become available by // time s while (occ.size() > 0 && occ.peek()[0] <= s) { let room = occ.pop()[1]; avail.push(room); } if (avail.size() > 0) { // Assign to the smallest available room let r = avail.pop(); occ.push([ e, r ]); cnt[r]++; } else { // All rooms are occupied; assign to the room // that becomes free earliest let earliest = occ.pop(); let t = earliest[0]; let r = earliest[1]; occ.push([ t + (e - s), r ]); cnt[r]++; } } // Find the room with the maximum number of meetings let maxCnt = 0; let res = 0; for (let i = 0; i < n; i++) { if (cnt[i] > maxCnt) { maxCnt = cnt[i]; res = i; } } return res; } //Driver Code Starts let n = 2; let meetings = [[ 0, 6 ], [ 2, 3 ], [ 3, 7 ], [ 4, 8 ], [ 6, 8 ] ]; console.log(mostBooked(n, meetings)); //Driver Code Ends
Similar Reads
Meeting rooms - Find minimum meeting rooms 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 meeti
15 min read
Maximum Meetings in One Room Given n meetings in the form of start[] and end[], where start[i] is the start time of ith meeting and end[i] is the end time of ith meeting. The task is to find the maximum number of meetings which can be held in a single room. The meeting room can have only one meeting at a particular time.Note: T
7 min read
Meeting Rooms - Check if a person can attend all meetings Given an array arr[][] such that arr[i][0] is the starting time of ith meeting and arr[i][1] is the ending time of ith meeting, the task is to check if it is possible for a person to attend all the meetings such that he can attend only one meeting at a particular time.Note: A person can also attend
10 min read
Find maximum number of people who can attend meeting There is only one room which is holding N meetings that are given as intervals of the form (start[i], end[i], people[i]) where start[i] is the starting time of the ith meeting, end[i] is the ending time of the ith meeting, people[i] is the number of people who can attend the ith meeting. At any poin
12 min read
Common Slot for Meeting of Two Persons You are given two lists of availability time slots, slt1[][] and slt2[][], for two people. Each slot is represented as [start, end], and it is guaranteed that within each list, no two slots overlap (i.e., for any two intervals, either start1>end2 or start2>end1). Given a meeting duration d, re
6 min read