Meeting Rooms

25 min · canAttendMeetings()

Calendars get awkward the moment one minute is promised to two meetings.

You're given intervals, where each [start, end] pair describes one meeting. Return true when one person can attend every meeting, or false when any two meetings overlap.

A few ground rules:

  • A meeting occupies time from start up to, but not including, end.
  • One meeting may end exactly when another begins. [0, 5] and [5, 10] don't overlap.
  • A meeting with start == end occupies no time, so it can't clash with another meeting.
  • An empty calendar is conflict-free and returns true.

Constraints

  • 0 <= intervals.length <= 100_000
  • Every interval contains exactly two integers: [start, end].
  • 0 <= start <= end <= 1_000_000_000

Hints

Start with pairs

Pick two meetings and ask whether the later start happens before the earlier end. Checking every pair is a correct baseline. How many checks does 100,000 meetings create?

Put the calendar in order

Sort meetings by start time. Once they're chronological, a conflict has nowhere to hide far away from the meeting that came just before it.

One strict comparison

After sorting, compare each non-empty meeting's start with the previous non-empty meeting's end. Use <, not <=, because touching endpoints are allowed.

Follow-up

If the answer is false, could you return one conflicting pair of meetings as well?

Visible cases

Examples

Example 1

ready
Input
intervals = [
  [
    0,
    30
  ],
  [
    5,
    10
  ],
  [
    15,
    20
  ]
]
Expected
false
Why
the 5–10 meeting starts while 0–30 is still running

Example 2

ready
Input
intervals = [
  [
    7,
    10
  ],
  [
    2,
    4
  ]
]
Expected
true
Why
the meetings occupy separate stretches of time

Example 3

ready
Input
intervals = []
Expected
true
Why
an empty calendar has no conflict

Interview signal

Asked at

AmazonGoogleMeta
Loading editor

Console

ready to run

Ready to run.

3 cases are queued.

Run: visible + custom · Submit: full suite