Browse curriculum

FAANGPrep Sprint

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

3 MEETINGS

INPUT

meetings · as given

[0,30]

[5,10]

[15,20]

0

30

Pairs clear

0

Answer

—

 

3 meetings: can one person attend them all?

Next

Sort by start time: [[0,30], [5,10], [15,20]] — a clash, if there is one, is now between neighbours

Meetings

Visualize checking for conflicts one by one.