Meeting Rooms Problem Minimum Rooms Required in DSA C++ - Time & Space Complexity
We want to find out how the time needed to find the minimum number of meeting rooms grows as the number of meetings increases.
Specifically, how does the code handle many meetings and overlapping times?
Analyze the time complexity of the following code snippet.
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int minMeetingRooms(vector<pair<int,int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const pair<int,int>& a, const pair<int,int>& b) { return a.first < b.first; });
priority_queue<int, vector<int>, greater<int>> minHeap;
for (auto &interval : intervals) {
if (!minHeap.empty() && minHeap.top() <= interval.first) {
minHeap.pop();
}
minHeap.push(interval.second);
}
return minHeap.size();
}
This code sorts meetings by start time, then uses a min-heap to track end times to find the minimum rooms needed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all meetings once after sorting.
- How many times: Exactly once for each meeting (n times).
- Additional operations: Sorting all meetings once before the loop (n log n), and heap operations inside the loop (push/pop) which take log n time each.
As the number of meetings grows, sorting takes more time, and each meeting causes heap operations.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Sort ~30 ops, Loop ~10 * log 10 ≈ 33 ops |
| 100 | Sort ~700 ops, Loop ~100 * log 100 ≈ 664 ops |
| 1000 | Sort ~10,000 ops, Loop ~1000 * log 1000 ≈ 10,000 ops |
Pattern observation: The total operations grow a bit faster than the number of meetings because of sorting and heap operations, roughly like n log n.
Time Complexity: O(n log n)
This means as the number of meetings grows, the time needed grows a bit faster than just counting them, mainly due to sorting and managing the heap.
[X] Wrong: "Since we just loop through meetings once, the time is O(n)."
[OK] Correct: Sorting the meetings first and using a heap inside the loop both add extra time, making the total time grow faster than just n.
Understanding this time complexity helps you explain how your solution handles many meetings efficiently, showing you know how sorting and heaps affect performance.
"What if the meetings were already sorted by start time? How would that change the time complexity?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of rooms needed so that no two meetings overlap in the same room.Step 2: Clarify what is being counted
We count how many meetings happen at the same time to find the minimum rooms required.Final Answer:
The minimum number of rooms needed so no meetings overlap -> Option CQuick Check:
Minimum rooms = max overlapping meetings [OK]
- Confusing total meetings with rooms needed
- Thinking earliest start time is the answer
- Ignoring overlapping intervals
Solution
Step 1: Identify sorting by start time
We want to sort intervals by their start time, which is stored in a.first.Step 2: Check lambda comparator correctness
std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a.first < b.first; }); uses a lambda comparing a.first < b.first, which sorts ascending by start time correctly.Final Answer:
std::sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a.first < b.first; }); -> Option BQuick Check:
Sort by start time ascending [OK]
- Sorting by end time instead of start time
- Using default sort without comparator (may sort by first but not guaranteed)
- Sorting descending when ascending is needed
std::vector> intervals = {{0,30}, {5,10}, {15,20}};
std::vector<int> starts, ends;
for(auto &i : intervals) {
starts.push_back(i.first);
ends.push_back(i.second);
}
std::sort(starts.begin(), starts.end());
std::sort(ends.begin(), ends.end());
int rooms = 0, end_ptr = 0;
for(int i = 0; i < starts.size(); i++) {
if(starts[i] < ends[end_ptr]) rooms++;
else end_ptr++;
}
std::cout << rooms << std::endl;Solution
Step 1: Sort start and end times
Starts = [0,5,15], Ends = [10,20,30]Step 2: Iterate and count rooms
i=0: 0<10 true rooms=1
i=1: 5<10 true rooms=2
i=2: 15<10 false end_ptr=1 rooms=2Final Answer:
2 -> Option AQuick Check:
Max overlapping meetings = 2 [OK]
- Not sorting start and end arrays
- Incrementing rooms incorrectly
- Confusing when to move end pointer
std::vector> intervals = {{1,5}, {2,6}, {8,10}};
std::sort(intervals.begin(), intervals.end());
int rooms = 1;
for(int i = 1; i < intervals.size(); i++) {
if(intervals[i].first < intervals[i-1].second) rooms++;
}
std::cout << rooms << std::endl;Solution
Step 1: Analyze overlap check logic
The code only checks if current meeting starts before previous meeting ends, ignoring overlaps with other meetings.Step 2: Understand why this causes error
This misses cases where a meeting overlaps with a meeting before the previous one, undercounting rooms needed.Final Answer:
It only compares each meeting with the previous one, missing overlaps with earlier meetings -> Option DQuick Check:
Check all overlaps, not just adjacent meetings [OK]
- Assuming sorting fixes all overlap checks
- Starting loop at wrong index
- Incorrect initial rooms count
{{0, 10}, {5, 15}, {10, 20}, {15, 25}, {20, 30}}, what is the minimum number of meeting rooms required to schedule all meetings without overlap?Solution
Step 1: Sort start and end times
Starts = [0,5,10,15,20], Ends = [10,15,20,25,30]Step 2: Use two pointers to count rooms
i=0: 0<10 rooms=1
i=1: 5<10 rooms=2
i=2: 10<10 false end_ptr=1 rooms=2
i=3: 15<15 false end_ptr=2 rooms=2
i=4: 20<20 false end_ptr=3 rooms=2Final Answer:
2 -> Option AQuick Check:
Max overlapping meetings = 2 [OK]
- Counting all meetings as overlapping
- Not moving end pointer correctly
- Misreading intervals causing wrong overlaps
Ready to prove it?
Pass the test and this pattern moves unknown
