Easy
LABMeeting Rooms
Given meeting intervals, return true if one person can attend every meeting without overlap.
EXAMPLES
Example 1
Input
{
"intervals": [
[
0,
30
],
[
5,
10
],
[
15,
20
]
]
}
Output
falseFUNCTION SHAPE
intervals: intMatrix→boolSOLUTION NOTE
Basically just consider all pairwise intervals, and check if there is overlap. (note: touching is not considered overlap in this case. Ie. [1,2], [2,3] are NOT overlapping) If there is overlap: ie. max(a,c) < min(b,d), then the user cannot attend all meetings, otherwise if there is no overlap between any pairwise meetings they can attend all meetings.
Reveal reference solution +
pythonREFERENCE
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
for (a, b), (c, d) in pairwise(sorted(intervals)):
if max(a, c) < min(b, d):
return False
return True
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
return not any(max(a, c) < min(b, d) for (a, b), (c, d) in pairwise(sorted(intervals)))Time
O(n log n)Space
O(sort)