LeetCode solutions

2479. Meeting Rooms III

My accepted C++ solution to LeetCode problem 2479, Meeting Rooms III, running in 69ms.

  • Difficulty: Hard
  • C++
  • Runtime 69ms
  • Memory 98.2MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 69ms, memory 98.2MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    int mostBooked(int n, vector<vector<int>>& meetings) {
        sort(meetings.begin(), meetings.end());
        vector<int> count(n, 0);
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> busy;
        priority_queue<int, vector<int>, greater<>> available;
        for (int i = 0; i < n; i++) available.push(i);
        for (auto& m : meetings) {
            long long start = m[0], end = m[1];
            while (!busy.empty() && busy.top().first <= start) {
                available.push(busy.top().second);
                busy.pop();
            }
            if (!available.empty()) {
                int room = available.top();
                available.pop();
                busy.push({end, room});
                count[room]++;
            } else {
                auto [endTime, room] = busy.top();
                busy.pop();
                busy.push({endTime + end - start, room});
                count[room]++;
            }
        }
        return max_element(count.begin(), count.end()) - count.begin();
    }
};

Source