- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 46 lines of C++ from the credited upstream file 2402.cpp.
- The implementation visibly relies on sequence storage, work queue.
- 3 loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1struct T {2 long endTime;3 int roomId;4};5 6class Solution {7 public:8 int mostBooked(int n, vector<vector<int>>& meetings) {9 vector<int> count(n);10 11 ranges::sort(meetings);12 13 auto compare = [](const T& a, const T& b) {14 return a.endTime == b.endTime ? a.roomId > b.roomId15 : a.endTime > b.endTime;16 };17 priority_queue<T, vector<T>, decltype(compare)> occupied(compare);18 priority_queue<int, vector<int>, greater<>> availableRoomIds;19 20 for (int i = 0; i < n; ++i)21 availableRoomIds.push(i);22 23 for (const vector<int>& meeting : meetings) {24 const int start = meeting[0];25 const int end = meeting[1];26 27 28 while (!occupied.empty() && occupied.top().endTime <= start)29 availableRoomIds.push(occupied.top().roomId), occupied.pop();30 if (availableRoomIds.empty()) {31 const auto [newStart, roomId] = occupied.top();32 occupied.pop();33 ++count[roomId];34 occupied.push({newStart + (end - start), roomId});35 } else {36 const int roomId = availableRoomIds.top();37 availableRoomIds.pop();38 ++count[roomId];39 occupied.push({end, roomId});40 }41 }42 43 return ranges::max_element(count) - count.begin();44 }45};46