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.
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 conflictExample 2:
Input: intervals = [(5,8),(9,15)]
Output: trueConstraints:
0 <= intervals.length <= 5000 <= intervals[i].start < intervals[i].end <= 1,000,000
You should aim for a solution with O(nlogn) time and O(n) space, where n is the size of the input array.
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.
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.
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.
Before attempting this problem, you should be comfortable with:
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:
In a brute force approach, we simply:
This approach is very straightforward and easy to understand, making it ideal as a starting solution.
n be the number of meetings.i from 0 to n - 1:j where j > i:A and B:min(A.end, B.end) > max(A.start, B.start)false immediatelytrue"""
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 TrueWe want to determine whether a person can attend all meetings without any overlaps.
A key observation is:
Why this works:
So by sorting once and doing a single pass, we can efficiently detect any conflict.
i1 be the previous meetingi2 be the current meetingi1.end > i2.start:falsetrue"""
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 TrueMeetings 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.
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.