CodingNodes / Intervals / Meeting Rooms System design Space play · ←→ step · R restart

Problem

Each meeting is a pair [start, end]: it begins at time start and finishes at time end. The array intervals lists all of them, in no particular order.

Return true if a single person could attend every meeting, and false if any two meetings overlap. A meeting that starts exactly when another one ends does not count as an overlap.

Constraints

  • 1 ≤ intervals.length ≤ 10⁴
  • intervals[i].length == 2
  • 0 ≤ start < end ≤ 10⁶

Examples: pick one to animate

Solutions

TimeO(n log n) SpaceO(1)

Time: Sorting costs O(n log n); the scan afterwards is a single O(n) pass.

Space: Only the loop index is stored (the sort is done in place; some sorts use O(log n) stack space).

Overlaps are hard to spot when meetings are in random order. After sorting by start time, a meeting can only clash with the one just before it: if it starts before the previous meeting ends, they overlap. Otherwise every earlier meeting has also ended. One pass over neighbors decides the answer.