Meeting Rooms - Problem

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

A person can attend all meetings if no two meetings overlap. Two meetings overlap if one starts before the other ends.

Example: If you have meetings [[0,30], [5,10], [15,20]], you cannot attend all meetings because the first meeting [0,30] overlaps with both [5,10] and [15,20].

Input & Output

Example 1 — Overlapping Meetings
$ Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
💡 Note: The first meeting [0,30] overlaps with both [5,10] and [15,20]. Since meeting [0,30] runs from time 0 to 30, it conflicts with meeting [5,10] which starts at time 5 (before 30 ends).
Example 2 — Non-overlapping Meetings
$ Input: intervals = [[7,10],[2,4]]
Output: true
💡 Note: After sorting by start time: [2,4] then [7,10]. Meeting [2,4] ends at time 4, and [7,10] starts at time 7. Since 4 < 7, there's no overlap.
Example 3 — Adjacent Meetings
$ Input: intervals = [[1,3],[3,6],[6,8]]
Output: true
💡 Note: Meetings end exactly when the next one starts: [1,3] ends at 3, [3,6] starts at 3. This is allowed - meetings can be back-to-back without overlap.

Constraints

  • 0 ≤ intervals.length ≤ 104
  • intervals[i].length == 2
  • 0 ≤ starti < endi ≤ 106

Visualization

Tap to expand
Meeting Rooms Problem INPUT Meeting Time Intervals 0 15 30 [0, 30] [5,10] [15,20] intervals = [[0,30],[5,10],[15,20]] Overlaps visible! ALGORITHM STEPS 1 Sort by Start Time Sort intervals by start [0,30],[5,10],[15,20] 2 Compare Adjacent Check if end[i] > start[i+1] 3 Check Overlap i=0: end=30, next start=5 30 > 5 ? YES! OVERLAP FOUND 4 Return Result If overlap: return false Time: O(n log n) | Space: O(1) FINAL RESULT Can attend all meetings? Output: false Meeting [0,30] overlaps with [5,10] and [15,20] Conflicts Detected: [0,30] vs [5,10] [0,30] vs [15,20] Key Insight: After sorting by start time, we only need to check if any meeting ends after the next one starts. If intervals[i].end > intervals[i+1].start, there's an overlap and we cannot attend all meetings. TutorialsPoint - Meeting Rooms | Optimal Solution
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
89.0K Views
High Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen