Sulba
000 / 100

Meeting Rooms

EasyTime O(n log n)Space O(1) besides the sortLeetCode 252 ↗

The problem

Given meeting times as intervals [start, end], return true if one person could attend them all — no two meetings overlap. A meeting may start the moment another ends.

Examples

01
Input
intervals = [[0, 30], [5, 10], [15, 20]]
Output
false
02
Input
intervals = [[7, 10], [2, 4]]
Output
true

Constraints

  • 0 ≤ intervals.length ≤ 10⁴
  • 0 ≤ start < end ≤ 10⁶

The idea

Sort the meetings by start time. If any two overlap, then some meeting and the one right after it in this order overlap — so only neighbours need checking.

Each meeting must end by the time the next begins.

Time
O(n log n)
Space
O(1) besides the sort

Solution · every language run against every case

class Solution:    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:        intervals.sort()  # in order of start: a clash can only be between neighbours        return all(intervals[i][1] <= intervals[i + 1][0] for i in range(len(intervals) - 1))