Meeting Rooms Room with Maximum Meetings (original) (raw)

Last Updated : 17 Oct, 2025

Given an integer **n and a 2D array meetings[][], where n represents the number of classrooms numbered from 0 to n - 1, and meetings[i] = [starti, endi] represents a meeting scheduled from start to end. Find the room number that hosts the most meetings. If multiple rooms have the same highest number of meetings, return the smallest room number among them.

**Meeting Allocation Rules

  1. When a meeting starts, assign it to the available room with the smallest number.
  2. If no rooms are free, delay the meeting until the earliest room becomes available. The delayed meeting retains its original duration.
  3. 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.

Try It Yourselfredirect icon

Table of Content

[Naive Approach] Using Sorting and Two arrays - O(n*m) Time and O(n) Space

The idea is to schedule meetings in rooms using two arrays: first for storing ending time of meetings in each room and second for the number of meetings per room. Sort the meetings by start time, assign each to the earliest available room, update its end time and count, and if no room is free, choose the one that becomes available soonest. Finally, check freq to find the room with the most meetings.

C++ `

#include #include #include using namespace std;

int mostBooked(int n, vector<vector> &meetings) { vector avail(n, 0); vector 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> meetings = {{0, 6}, {2, 3}, {3, 7}, {4, 8}, {6, 8}}; cout << mostBooked(n, meetings); }

Java

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

class GfG { 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

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#

using System;

class GfG {

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

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;

} //Driver Code 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 idea is sort meetings by start time and use two min-heaps — one for free rooms (smallest number first) and one for ongoing meetings (earliest end time first). For each meeting, free up rooms that have finished, assign it to a free room if available, or to the room that frees earliest if none are free. Track meeting counts and return the room with the highest count.

C++ `

//Driver Code Starts #include #include #include #include using namespace std; //Driver Code Ends

int mostBooked(int n, vector<vector> &meetings) {

// Count of meetings per room
vector<int> cnt(n, 0);

// Min-heap for occupied rooms
priority_queue<pair<int, int>, vector<pair<int, int>>, 
                                  greater<pair<int, int>>> occ;

// Min-heap for available rooms
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;

}

//Driver Code Starts int main() { int n = 2; vector<vector> meetings = {{0, 6}, {2, 3}, {3, 7}, {4, 8}, {6, 8}}; cout << mostBooked(n, meetings); } //Driver Code Ends

Java

//Driver Code Starts import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue;

class GfG { //Driver Code Ends

 static int  mostBooked(int n, int[][] meetings) {
     
       // Count of meetings per room
    int[] cnt = new int[n]; 

    // PriorityQueue for occupied rooms
    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
    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]; 
        int e = m[1];

        // 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;
}

//Driver Code Starts 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)); } } //Driver Code Ends

Python

#Driver Code Starts

import heapq #Driver Code Ends

def mostBooked(n, meetings):

  # Count of meetings per room
cnt = [0] * n 

# Min-heap for occupied rooms
occ = []

# Min-heap for available rooms
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

#Driver Code Starts if name == "main": n = 2 meetings = [[0, 6], [2, 3], [3, 7], [4, 8], [6, 8]] print(mostBooked(n, meetings))

#Driver Code Ends

C#

//Driver Code Starts using System; using System.Collections.Generic;

class Pair { public int first; public int second;

public Pair(int first, int second) {
    this.first = first;
    this.second = second;
}

}

class ComparerOcc : IComparer { 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 { public int Compare(int a, int b) { return a - b; } }

// Custom Priority queue class PriorityQueue { private List heap; private IComparer 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;
}

} //Driver Code Ends

class GfG { static int mostBooked(int n, int[][] meetings) {

    // Count of meetings per room
    int[] cnt = new int[n];

    // Min-heap for occupied rooms
    PriorityQueue<Pair> occ = new PriorityQueue<Pair>(new ComparerOcc());

    // Min-heap for available rooms
    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;
}

//Driver Code Starts 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)); } }

//Driver Code Ends

JavaScript

//Driver Code Starts 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
const occ = new PriorityQueue((a, b) => {
    if (a[0] !== b[0]) {
        return a[0] < b[0]; 
    }
    return a[1] < b[1]; 
                        
});

// Priority Queue for available rooms
const avail = new PriorityQueue(
    (a, b) => a < b); 
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 //Driver Code let n = 2; let meetings = [[ 0, 6 ], [ 2, 3 ], [ 3, 7 ], [ 4, 8 ], [ 6, 8 ] ]; console.log(mostBooked(n, meetings)); //Driver Code Ends

`