252. Meeting Rooms - Explanation
Description
Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],...] (start_i < end_i), determine if a person could add all meetings to their schedule without any conflicts. The intervals may be provided in any order.
Note: (0,8),(8,10) is not considered a conflict at 8
Example 1:
Input: intervals = [(0,30),(5,10),(15,20)]
Output: falseExplanation:
(0,30)and(5,10)will conflict(0,30)and(15,20)will conflict
Example 2:
Input: intervals = [(5,8),(9,15)]
Output: trueConstraints:
0 <= intervals.length <= 5000 <= intervals[i].start < intervals[i].end <= 1,000,000
Topics
Recommended Time & Space Complexity
You should aim for a solution with O(nlogn) time and O(n) space, where n is the size of the input array.
Hint 1
If two intervals are sorted in ascending order by their start values, they overlap if the start value of the second interval is less than the end value of the first interval. And these are called overlapping intervals.
Hint 2
A brute force approach would be to check every pair of intervals and return false if any overlap is found. This would be an O(n^2) solution. Can you think of a better way? Maybe you should visualize the given intervals as line segments.
Hint 3
We should sort the given intervals based on their start values, as this makes it easier to check for overlaps by comparing adjacent intervals. We then iterate through the intervals from left to right and return false if any adjacent intervals overlap.
Prerequisites
Before attempting this problem, you should be comfortable with:
- Sorting - Required to process meetings in order and enable efficient overlap detection
- Intervals - Understanding how to represent and compare time intervals
- Overlap Detection - Knowing how to determine if two intervals share common time
1. Brute Force
Intuition
We want to check whether a person can attend all meetings without any overlap.
Two meetings overlap if they share any common time.
For two intervals A and B, this happens when:
- the earlier ending time is greater than the later starting time
In a brute force approach, we simply:
- compare every pair of meetings
- if any pair overlaps, it is impossible to attend all meetings
This approach is very straightforward and easy to understand, making it ideal as a starting solution.
Algorithm
- Let
nbe the number of meetings. - For each meeting
ifrom0ton - 1: - Compare it with every meeting
jwherej > i: - For meetings
AandB:- Check if they overlap using:
min(A.end, B.end) > max(A.start, B.start)
- Check if they overlap using:
- If an overlap is found:
- return
falseimmediately
- return
- If no overlapping pair is found after checking all pairs:
- return
true
- return
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def canAttendMeetings(self, intervals: List[Interval]) -> bool:
n = len(intervals)
for i in range(n):
A = intervals[i]
for j in range(i + 1, n):
B = intervals[j]
if min(A.end, B.end) > max(A.start, B.start):
return False
return TrueTime & Space Complexity
- Time complexity:
- Space complexity:
2. Sorting
Intuition
We want to determine whether a person can attend all meetings without any overlaps.
A key observation is:
- if meetings are sorted by their start time, then
- we only need to check adjacent meetings for overlap
Why this works:
- if two meetings overlap, they must appear next to each other after sorting by start time
- there is no need to compare every pair
So by sorting once and doing a single pass, we can efficiently detect any conflict.
Algorithm
- Sort all meetings by their start time.
- Iterate through the sorted list starting from the second meeting:
- For each pair of adjacent meetings:
- let
i1be the previous meeting - let
i2be the current meeting
- let
- If
i1.end > i2.start:- the meetings overlap
- return
false
- If the loop finishes without finding any overlap:
- return
true
- return
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def canAttendMeetings(self, intervals: List[Interval]) -> bool:
intervals.sort(key=lambda i: i.start)
for i in range(1, len(intervals)):
i1 = intervals[i - 1]
i2 = intervals[i]
if i1.end > i2.start:
return False
return TrueTime & Space Complexity
- Time complexity:
- Space complexity: or depending on the sorting algorithm.
Common Pitfalls
Using the Wrong Overlap Condition
Meetings overlap when prev.end > curr.start, not prev.end >= curr.start. If one meeting ends exactly when another starts, they do not overlap and can both be attended.
Forgetting to Sort Before Comparing Adjacent Meetings
The sorting approach only works when meetings are sorted by start time. Comparing adjacent meetings in an unsorted list will miss overlaps between non-adjacent intervals.
Sign in to join the discussion