Meeting Rooms
Problem
Given an array of meeting time intervals where
intervals[i] = [starti, endi], determine if a person could attend all meetings.
Examples
Example 1
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
[0,30] overlaps with [5,10] and [15,20].
Example 2
Input: intervals = [[7,10],[2,4]]
Output: true
Key Insight
Sort by start time. Check if any meeting starts before the previous one ends.
Sort by start time. Check if any meeting starts before the previous ends. One overlap = can't attend all.
How to Approach This Problem
Pattern Recognition: When you see keywords like
meeting rooms intervals,
think Intervals.
Step-by-Step Reasoning
1
A person can attend all meetings if:
Answer: No two meetings overlap
Can't be in two places at once. No overlaps means all are attendable.
2
After sorting by start time, we only need to check:
Answer: Consecutive pairs of meetings
If sorted, meeting i can only overlap with i+1. If i overlaps with j (j > i+1), it must overlap with i+1 first.
3
Sorted meetings [a, b] and [c, d] (where a <= c) overlap if:
Answer: b > c
If meeting 1 ends after meeting 2 starts, overlap. (b > c, not >=, since [1,2],[2,3] don't overlap)
4
If we find one pair of overlapping meetings:
Answer: Return false immediately
One overlap is enough to fail. Can't attend both.
5
If 0 or 1 meetings:
Answer: Return true
No meetings or one meeting means no possible conflicts.
Solution
def canAttendMeetings(intervals: List[List[int]]) -> bool:
if len(intervals) <= 1:
return True
# Sort by start time
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
# If current starts before previous ends
if intervals[i][0] < intervals[i - 1][1]:
return False
return True
Complexity Analysis
| Time | O(n log n) |
|---|---|
| Space | O(1) |
Master This Pattern
Build intuition with interactive MCQs. Practice Intervals problems in the LeetEye app.
Download LeetEye Free