Browse curriculum

Meeting Rooms

Determine if a person can attend all meetings.

Problem Understanding

Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], determine if a person can attend all meetings.

Example 1:

  • Input: [[0,30],[5,10],[15,20]]
  • Output: false
  • Why? Meeting [0,30] overlaps with both [5,10] and [15,20].

Example 2:

  • Input: [[7,10],[2,4]]
  • Output: true

Note: LeetCode 252, linked above, is a LeetCode Premium problem — opening it needs a Premium subscription. Everything needed to solve it is on this page.

Algorithm Strategy

We simply need to check if any two intervals overlap. If we sort them by start time, we only need to check adjacent meetings.

  1. Sort: Sort intervals by start time.
  2. Scan: Iterate through the sorted list.
  3. Check: If current.start < previous.end, then there is an overlap. Return false immediately.
  4. Result: If loop finishes without conflict, return true.

Interactive Visualization

Visualize checking for conflicts one by one.

Dry Run

Input: [[0,30], [5,10], [15,20]]

Step Comparison Overlap? Action
Init Sort - [[0,30], [5,10], [15,20]]
1 [0,30] vs [5,10] Yes (5 < 30) Return false
Final - - False

Edge Cases & Common Mistakes

  • Empty Input: [] -> true.
  • Single Meeting: [[1,2]] -> true.
  • Touching: [[1,2], [2,3]] -> true. (Meeting 2 starts exactly when 1 ends).

Solution

Simple sorting solution O(N log N).

Meeting Rooms Code
1var canAttendMeetings = function(intervals) {
2 intervals.sort((a, b) => a[0] - b[0]);
3 for (let i = 1; i < intervals.length; i++) {
4 if (intervals[i][0] < intervals[i - 1][1]) {
5 return false;
6 }
7 }
8 return true;
9};

The approach, step by step

  1. Sort by start

    Sort the meetings by start time.

  2. Compare neighbours

    Return false as soon as a meeting starts before the previous meeting ends.

  3. Return true

    If no neighbours overlap, every meeting can be attended.

Frequently asked questions

How do you check whether all meetings can be attended?

Sort the meetings by start time. If any meeting starts before the previous one ends, two meetings overlap and the answer is false; if none does, it is true.


Why is comparing neighbours enough after sorting?

If a meeting overlaps an earlier one that is not its neighbour, it also starts before its neighbour ends, because the neighbour started later. So a clash always shows up between adjacent meetings.


Is LeetCode 252 Meeting Rooms free?

No, it is a LeetCode Premium problem. This lesson states the full problem with examples, so it can be solved without Premium.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description