Meeting Rooms Problem Minimum Rooms Required in DSA C# - Time & Space Complexity
We want to know how the time needed to find the minimum number of meeting rooms grows as the number of meetings increases.
How does the program handle more meetings and still find the answer efficiently?
Analyze the time complexity of the following code snippet.
public int MinMeetingRooms(int[][] intervals) {
Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));
var heap = new PriorityQueue<int, int>();
foreach (var interval in intervals) {
if (heap.Count > 0 && interval[0] >= heap.Peek()) {
heap.Dequeue();
}
heap.Enqueue(interval[1], interval[1]);
}
return heap.Count;
}
This code sorts meetings by start time, then uses a structure to track ongoing meetings by their end times to find the minimum rooms needed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all meetings once.
- How many times: Once for each meeting (n times).
- Inside loop: Checking and updating the heap (log n time each).
As the number of meetings grows, the sorting step takes more time, and each meeting requires checking and updating the heap.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Sorting ~10 log 10 + 10 heap ops |
| 100 | Sorting ~100 log 100 + 100 heap ops |
| 1000 | Sorting ~1000 log 1000 + 1000 heap ops |
Pattern observation: The time grows a bit more than linearly because of sorting and heap updates, roughly n log n.
Time Complexity: O(n log n)
This means as meetings increase, the time grows a bit faster than just counting them, mainly due to sorting and managing the heap.
[X] Wrong: "The time is just O(n) because we only loop once through meetings."
[OK] Correct: Sorting the meetings first and managing the heap inside the loop both take extra time, making the total time more than just one loop.
Understanding this time complexity helps you explain how your solution scales and shows you can analyze real problems efficiently.
"What if we used a simple list instead of a heap to track end times? How would the time complexity change?"
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 rooms, not meetings or durations. The goal is to avoid time conflicts in the same room.Final Answer:
The minimum number of meeting rooms needed to hold all meetings without overlap -> Option DQuick Check:
Minimum rooms = no overlapping meetings at the same time [OK]
- Confusing number of meetings with number of rooms
- Thinking about meeting durations instead of overlaps
- Ignoring overlapping intervals
Solution
Step 1: Check tuple list initialization syntax
var meetings = new List<(int start, int end)> { (9, 10), (11, 12) }; correctly uses List of tuples with named elements and proper tuple syntax.Step 2: Identify errors in other options
var meetings = new List { [9, 10], [11, 12] }; uses invalid array syntax inside list initialization. var meetings = new List<(int, int)> { 9, 10, 11, 12 }; tries to add ints directly, not tuples. var meetings = new List<(int start, int end)> { (start: 9, end: 10), (start: 11) }; misses end value in second tuple.Final Answer:
var meetings = new List<(int start, int end)> { (9, 10), (11, 12) }; -> Option CQuick Check:
Correct tuple list syntax = var meetings = new List<(int start, int end)> { (9, 10), (11, 12) }; [OK]
- Using square brackets instead of parentheses for tuples
- Missing tuple elements or names
- Adding ints instead of tuples
var intervals = new List<(int start, int end)>{ (0, 30), (5, 10), (15, 20) };
int minRooms = MinMeetingRooms(intervals);
Console.WriteLine(minRooms);
int MinMeetingRooms(List<(int start, int end)> intervals) {
var starts = intervals.Select(i => i.start).OrderBy(t => t).ToList();
var ends = intervals.Select(i => i.end).OrderBy(t => t).ToList();
int rooms = 0, endIndex = 0;
foreach (var start in starts) {
if (start < ends[endIndex]) rooms++;
else endIndex++;
}
return rooms;
}Solution
Step 1: Sort start and end times
Starts = [0, 5, 15], Ends = [10, 20, 30]Step 2: Iterate starts comparing with ends
start=0 < 10 -> rooms=1; start=5 < 10 -> rooms=2; start=15 >= 10 -> endIndex=1, no increment rooms; final rooms=2Final Answer:
2 -> Option BQuick Check:
Overlapping intervals = 2 rooms needed [OK]
- Not sorting start and end times separately
- Incrementing rooms incorrectly when meetings don't overlap
- Confusing start and end comparisons
int MinRooms(List<(int start, int end)> intervals) {
intervals.Sort((a, b) => a.start.CompareTo(b.start));
var heap = new SortedSet<int>();
foreach (var interval in intervals) {
if (heap.Count > 0 && interval.start >= heap.Min) {
heap.Remove(heap.Min);
}
heap.Add(interval.end);
}
return heap.Count;
}Solution
Step 1: Understand SortedSet behavior
SortedSet does not allow duplicates, so multiple meetings ending at same time cause missing removals.Step 2: Identify impact on room count
Without duplicates, heap may keep extra end times, inflating room count incorrectly.Final Answer:
SortedSet does not allow duplicate end times, causing incorrect room count -> Option AQuick Check:
Data structure must allow duplicates for correct room count [OK]
- Using SortedSet instead of a priority queue or min-heap
- Confusing start and end time comparisons
- Incorrect condition for removing from heap
[(1, 4), (2, 5), (7, 9), (3, 6), (10, 11)]. Using the minimum rooms required approach, what is the minimum number of rooms needed to schedule all meetings without overlap?Solution
Step 1: Sort start and end times
Starts = [1, 2, 3, 7, 10], Ends = [4, 5, 6, 9, 11]Step 2: Count overlaps using two pointers
start=1 < 4 rooms=1; start=2 < 4 rooms=2; start=3 < 4 rooms=3; start=7 >= 4 endIndex=1 no increment rooms; start=10 >= 5 endIndex=2 no increment rooms; final rooms=3Final Answer:
3 -> Option AQuick Check:
Maximum overlaps = 3 rooms needed [OK]
- Not sorting intervals before processing
- Counting rooms incorrectly when meetings end
- Ignoring overlapping intervals
Ready to prove it?
Pass the test and this pattern moves unknown
