Meeting Rooms Problem Minimum Rooms Required in DSA Python - Time & Space Complexity
We want to find out how the time needed to find the minimum number of meeting rooms changes as the number of meetings grows.
Specifically, how does the code handle more meetings efficiently?
Analyze the time complexity of the following code snippet.
from heapq import heappush, heappop
def minMeetingRooms(intervals):
intervals.sort(key=lambda x: x[0])
heap = []
for interval in intervals:
if heap and heap[0] <= interval[0]:
heappop(heap)
heappush(heap, interval[1])
return len(heap)
This code finds the minimum number of meeting rooms needed by sorting meetings and using a heap to track end times.
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: Heap operations (push and sometimes pop) happen for each meeting.
As the number of meetings increases, the sorting and heap operations grow too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 + 10 operations |
| 100 | About 100 log 100 + 100 operations |
| 1000 | About 1000 log 1000 + 1000 operations |
Pattern observation: The time grows a bit faster than just the number of meetings because of sorting and heap operations, but not as fast as checking every pair.
Time Complexity: O(n log n)
This means the time needed grows a little more than linearly as meetings increase, mainly due to sorting and managing the heap.
[X] Wrong: "The code just loops through meetings once, so it must be O(n)."
[OK] Correct: Sorting the meetings first and using a heap for end times adds extra work that grows with n log n, not just n.
Understanding this time complexity helps you explain how your solution scales and why using sorting plus a heap is efficient for scheduling problems.
"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.Final Answer:
The minimum number of meeting rooms needed to hold all meetings without overlap -> Option AQuick Check:
Minimum rooms = rooms needed to avoid overlap [OK]
- Confusing total meetings with rooms needed
- Ignoring overlapping intervals
- Counting meeting duration instead of rooms
Solution
Step 1: Identify sorting by start time
Each interval is a list or tuple like [start, end]. Sorting by x[0] sorts by start time.Step 2: Check other options
Sorting by x[1] sorts by end time, sort() without key sorts lexicographically (primarily by start but also end), lambda x: x sorts by whole interval.Final Answer:
intervals.sort(key=lambda x: x[0]) -> Option BQuick Check:
Sort by start time = key=lambda x: x[0] [OK]
- Sorting by end time instead of start time
- Using sort() without key on lists of lists
- Confusing lambda syntax
intervals = [[0, 30], [5, 10], [15, 20]]
starts = sorted([i[0] for i in intervals])
ends = sorted([i[1] for i in intervals])
rooms = 0
end_ptr = 0
for start in starts:
if start < ends[end_ptr]:
rooms += 1
else:
end_ptr += 1
print(rooms)Solution
Step 1: Sort start and end times
starts = [0,5,15], ends = [10,20,30]. Initialize rooms=0, end_ptr=0.Step 2: Trace the loop
- start=0 < ends[0]=10: yes, rooms=1 (end_ptr=0)
- start=5 < ends[0]=10: yes, rooms=2 (end_ptr=0)
- start=15 < ends[0]=10: no, end_ptr=1
Final rooms=2.Final Answer:
2 -> Option CQuick Check:
Maximum concurrent meetings = 2 [OK]
- Not moving end pointer correctly
- Counting rooms too many times
- Mixing start and end comparisons
def minMeetingRooms(intervals):
starts = sorted([i[0] for i in intervals])
ends = sorted([i[1] for i in intervals])
rooms = 0
end_ptr = 0
for start in starts:
if start > ends[end_ptr]:
end_ptr += 1
else:
rooms += 1
return rooms
print(minMeetingRooms([[0, 30], [5, 10], [15, 20]]))Solution
Step 1: Analyze the comparison logic
The code uses if start > ends[end_ptr] to advance end_ptr, but correct logic requires advancing when start >= ends[end_ptr] (or equivalently, increment rooms when start < ends[end_ptr]).Step 2: Understand impact
Wrong comparison fails when start == ends[end_ptr], incorrectly counting extra room.Final Answer:
The comparison should be start < ends[end_ptr], not start > ends[end_ptr] -> Option AQuick Check:
Correct comparison fixes room count [OK]
- Using wrong comparison operator
- Not sorting ends list
- Forgetting to increment end pointer
[[1, 4], [2, 5], [7, 9], [3, 6], [10, 11]], what is the minimum number of meeting rooms required 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: Simulate two-pointer algorithm
rooms=0, end_ptr=0
- start=1 < 4: rooms=1
- start=2 < 4: rooms=2
- start=3 < 4: rooms=3
- start=7 < 4: no, end_ptr=1
- start=10 < 5: no, end_ptr=2
The final rooms value (3) is the max concurrent meetings needed.Final Answer:
3 -> Option DQuick Check:
Max overlap (e.g., [1,4],[2,5],[3,6]) = 3 [OK]
- Not updating end pointer correctly
- Counting all meetings as overlapping
- Ignoring meeting end times
Ready to prove it?
Pass the test and this pattern moves unknown
