Search lands in PR-5.1 (Pagefind).

How-to Beginner

Chapter 1 Updated

Meeting Rooms I — Conflict Detection

Sort by start, check consecutive pairs — done.

  • Full 4m
  • Revision 2m
  • Flow 1m

Problem Statement

Given an array of meeting time intervals, determine whether a single person can attend all meetings — that is, whether any two meetings overlap.

Input

An array of intervals [[start, end], …] representing meeting times.

Example inputs
[[0,30],[5,10],[15,20]]
[[7,10],[2,4]]

Output

A boolean: true if the person can attend every meeting, false if any two meetings conflict.

Expected outputs
false
true

Constraints

  • 0 ≤ intervals.length ≤ 10⁴
  • intervals[i].length == 2
  • 0 ≤ startᵢ < endᵢ ≤ 10⁶

Intuition

Sort by start time. After sorting, any overlapping meetings must sit next to each other. Walk through consecutive pairs — if one starts before the previous one ends, there’s a conflict.

Why consecutive pairs are enough
Before sort:  [0,30] [5,10] [15,20]
After sort:   [0,30] [5,10] [15,20]
               └─────┘
               5 < 30 → conflict detected immediately

Solution

meeting_rooms_i.py
def canAttendMeetings(intervals: list[list[int]]) -> bool:
    intervals.sort(key=lambda x: x[0])
 
    for i in range(1, len(intervals)):
        if intervals[i][0] < intervals[i - 1][1]:
            return False
 
    return True

Complexity

MetricValueReason
TimeO(n log n)Dominated by sorting; the linear scan is O(n)
SpaceO(1)In-place sort, no extra structure

Edge Case Thinking

ScenarioHandling
0 or 1 meetingAlways true — no possible conflict.
Back-to-back [1,2] & [2,3]2 < 2 is false → no conflict (half-open semantics). For closed semantics, use .
All at the same time — [[1,5],[1,5],[1,5]]First pair already conflicts → return false.
Already sorted non-overlapping inputLoop runs, all comparisons pass, return true.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.