Back to Interval
Interval
Easy

Meeting Rooms

LAB

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
false

FUNCTION SHAPE

intervals: intMatrixbool
SOLUTION 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)))
TimeO(n log n)
SpaceO(sort)
Open on LeetCode
00:00
3 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.