Skip to main content
63

Meeting Rooms

Easyintervalssorting

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

This problem uses:IntervalsRead the beginner guide

Given an array of meeting time intervals, where intervals[i] = [start, end], return true if one person could attend all the meetings, meaning no two of them overlap.

Understand the problem

  • An interval [start, end] is a stretch of time from start up to end.
  • Two meetings overlap if one starts before the other has ended.
  • Meetings that only touch, like [1, 2] and [2, 3], do not overlap: you can walk straight from one to the next.
  • The meetings are not given in time order.

Example 1

text
Input:  intervals = [[0, 30], [5, 10], [15, 20]]
Output: false

[5, 10] starts while [0, 30] is still going.

Example 2

text
Input:  intervals = [[7, 10], [2, 4]]
Output: true

In time order: [2, 4], then [7, 10]. No clash.

Example 3

text
Input:  intervals = []
Output: true

No meetings, nothing to clash.

Constraints

  • 0 <= intervals.length <= 10^4
  • 0 <= start < end <= 10^6

Hints

Stuck? Open one hint at a time. Each one gives away a little more than the last.

    The idea in one sentence

    Sort the meetings by start time, then check that each one starts no earlier than the previous one ends.

    The naive approach (and why it's slow)

    Compare every pair of meetings: does one start before the other ends? That's about n²/2 comparisons, O(n²).

    The key insight: sort by start

    This is the first move in almost every interval problem. Once the meetings are in time order, a meeting can only clash with its direct neighbor before it.

    Why? Picture a timeline, and say B comes right after A (sorted by start). If some earlier meeting reached into B, it would still be running when B starts, which is at or after A's start. So it would already overlap A, and we'd have caught that clash first.

    So:

    1. Sort by start.
    2. For each meeting after the first: if start < previous end, return false.
    3. If you get through them all, return true.

    Step by step

    intervals = [[0, 30], [5, 10], [15, 20]]. Sorted by start: the same order.

    text
    time:  0    5    10   15   20        30
           [==================================]   [0, 30]
                [====]                            [5, 10]
                          [====]                  [15, 20]
    MeetingPrevious ends atStarts before that?Result
    [0, 30](first)
    [5, 10]305 < 30: yesreturn false

    The code

    The reference solution is below, with comments on every important line.

    How fast is it?

    • Time: O(n log n) for the sort. The check afterward is O(n).
    • Memory: O(n) for the sorted copy.

    Common mistakes

    • Using <= and treating touching meetings ([1, 2], [2, 3]) as a clash.
    • Forgetting to sort. Without sorting, comparing neighbors misses clashes between meetings that are far apart in the input.
    • Sorting by end here. It also works for this problem, but sorting by start is the habit to build.

    Going deeper

    • Meeting Rooms II asks how many rooms you'd need if meetings can overlap.
    • "Sort, then compare each interval with the previous one" is the backbone of Merge Intervals too.

    Reference solution

    js
    /**
     * @param {number[][]} intervals
     * @return {boolean}
     */
    export default function canAttendMeetings(intervals) {
      // In start order, a meeting can only clash with the one right before it.
      const sorted = [...intervals].sort((a, b) => a[0] - b[0])
      for (let i = 1; i < sorted.length; i++) {
        // Starts before the previous one ends? (Touching, start === end, is fine.)
        if (sorted[i][0] < sorted[i - 1][1]) return false
      }
      return true
    }
    ts
    export default function canAttendMeetings(intervals: number[][]): boolean {
      // In start order, a meeting can only clash with the one right before it.
      const sorted = [...intervals].sort((a, b) => a[0] - b[0])
      for (let i = 1; i < sorted.length; i++) {
        // Starts before the previous one ends? (Touching, start === end, is fine.)
        if (sorted[i][0] < sorted[i - 1][1]) return false
      }
      return true
    }
    python
    def can_attend_meetings(intervals: list[list[int]]) -> bool:
        # In start order, a meeting can only clash with the one right before it.
        intervals = sorted(intervals)
        # Each meeting must start no earlier than the previous one ends (touching is fine).
        return all(intervals[i][0] >= intervals[i - 1][1] for i in range(1, len(intervals)))