Meeting Rooms
Table of Contents + β
Meeting Rooms is the gentlest interval question. You have a calendar of meetings. You just need to answer one yes or no question: can one person attend all of them without a clash? It sounds easy, and it is, once you sort. This question is a warm up for the harder Meeting Rooms II.
π― The Problem
You get a list of meetings and answer one yes or no question.
- Each meeting is a pair: a start time and an end time.
- Return true if one person can attend every meeting.
- They can attend all only if no two meetings overlap in time.
- Two meetings overlap when one starts before the other ends.
- Touching at a point is fine. A meeting ending at
30and another starting at30do not clash.
Input: intervals = [[0, 30], [5, 10], [15, 20]]Output: false
Explanation: [0, 30] overlaps with [5, 10], so the personcannot attend both. The answer is false.Here is the input drawn on a number line. You can see the long [0, 30] meeting covers the times of both other meetings, so there is a clash.
So the answer is a simple true or false. True means no clashes. False means at least one clash.
π’ Approach 1: Check Every Pair (Brute Force)
Compare every meeting against every other meeting.
The idea:
- Take each pair of meetings.
- Check if the two overlap.
- If any pair clashes, return false. If none clash, return true.
How it works:
- Two nested loops walk all pairs.
- Overlap means one starts before the other ends.
Why it is weak:
- It checks every pair, so the work is O(nΒ²).
- A big calendar makes it slow.
- It does far more comparisons than needed.
Here is the check-every-pair code:
def can_attend_meetings(intervals): for i in range(len(intervals)): for j in range(i + 1, len(intervals)): a_start, a_end = intervals[i] b_start, b_end = intervals[j] if max(a_start, b_start) < min(a_end, b_end): return False return Trueβ‘ Approach 2: Sort Then Scan Neighbors (Best)
The idea in one line: sort by start, then a clash can only show up between neighbors.
The idea:
- Sort the meetings by start time.
- After sorting, overlaps sit right next to each other.
- So you only compare each meeting with the one before it.
How it works:
- Walk from the second meeting to the last.
- Ask: does this meeting start before the previous one ended?
- If yes, that is a clash, so return false at once.
- If you reach the end with no clash, return true.
Why it is fast:
- Sorting costs O(n log n). The single scan is O(n).
- The sort is the heavy part, so the total is O(n log n).
- No far-apart comparisons, only neighbors.
Here is the scan as a decision flow across sorted neighbors.
Steps to Solve
- Sort the meetings by their start time.
- Walk from the second meeting to the last.
- For each meeting, compare its start with the end of the meeting before it.
- If the start is less than the previous end, there is a clash, so return false.
- If the loop finishes with no clash, return true.
This Python version sorts by start, then scans each neighbor pair.
def can_attend_meetings(intervals): intervals.sort(key=lambda block: block[0]) # sort by start for i in range(1, len(intervals)): if intervals[i][0] < intervals[i - 1][1]: # starts before prev end return False # clash found return True
intervals = [[0, 30], [5, 10], [15, 20]]print(can_attend_meetings(intervals))The output of the above code will be:
FalseNow let us walk through the Python version line by line and see why each part is written this way.
def can_attend_meetings(intervals): intervals.sort(key=lambda block: block[0])We sort by block[0], the start time. This is the trick. Once meetings are in start order, a clash can only happen between a meeting and the one right before it. So we never need to compare meetings that are far apart.
for i in range(1, len(intervals)): if intervals[i][0] < intervals[i - 1][1]: return FalseWe loop from index 1 so we always have a previous meeting at i - 1. The check intervals[i][0] < intervals[i - 1][1] reads as βthis meeting starts before the previous one ended.β That is a clash. The moment we find one clash, the answer is settled, so we return False right away and stop.
return TrueIf the loop finishes with no clash, every meeting started on or after the previous one ended. So one person can attend them all. We return True. Notice we use < and not <=, because a meeting starting exactly when another ends does not clash.
β±οΈ Time and Space Complexity
The brute force checks every pair, so it is O(nΒ²). The sort then scan costs O(n log n) for the sort plus O(n) for the scan. The sort dominates, so the total is O(n log n). Space is O(1) beyond the input, since we only compare neighbors and keep no extra structure. The big idea is that sorting lets a single neighbor scan answer the whole question.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute force, check every pair | O(nΒ²) | O(1) |
| Sort then scan neighbors | O(n log n) | O(1) |
Tip
Use strictly less than for the clash check. A meeting that begins exactly when another ends is fine, so < is correct and <= would give a wrong false.
π§© Key Takeaways
- β The answer is just true or false: can one person attend every meeting?
- β Sort by start, then a clash can only appear between neighbors.
- β A clash means a meeting starts before the previous one ended.
- β Return false on the first clash you find.
- β Use strictly less than, because touching at a point is not a clash.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does the Meeting Rooms problem ask you to return?
Why: It is a yes or no question: can a single person attend every meeting with no overlap?
- 2
After sorting by start, where can a clash appear?
Why: Once meetings are in start order, an overlap can only happen between neighbors, so one scan is enough.
- 3
Which comparison correctly detects a clash?
Why: A clash means the current meeting starts strictly before the previous one ends, so we use less than.
- 4
What is the time complexity of the sort then scan solution?
Why: The sort costs O(n log n) and dominates the linear neighbor scan, so the total is O(n log n).