Determine whether one person can attend every meeting without overlapping commitments in the schedule.
Problem
Input. A list intervals of integer meeting pairs [start, end], with start < end.
Output. Return True if one person can attend all meetings in full, and False if any meetings conflict.
Rules. Meetings occupy half-open intervals [start, end).
Ending at the next meeting’s start is allowed; duplicate positive-duration meetings conflict.
The reference may reorder the outer list but does not alter its rows.
An empty schedule returns True. Zero-duration meetings are outside the contract.
Example. [[0, 30], [5, 10], [15, 20]] returns False because the first meeting overlaps the others.
For [[0, 5], [5, 10]], return True.
Picture
For [[7, 10], [2, 4], [12, 14], [9, 11]], chronological order becomes [2,4], [7,10], [9,11], [12,14].
4 <= 7 passes; 10 <= 9 fails, proving overlap during [9,10).
Skip the final boundary.
Problem trace
One overlapping adjacent pair is enough to reject attendance
- max
- 14
- input
- [[7,10],[2,4],[12,14],[9,11]]
- index 0
- [7,10]
- sorted
- not shown→[[2,4],[7,10],[9,11],[12,14]]
- input
- [[7,10],[2,4],[12,14],[9,11]]→not shown
- index 0
- [7,10]→[2,4]
- index
- not shown→1
- boundary
- not shown→4 <= 7: true
- sorted
- [[2,4],[7,10],[9,11],[12,14]]→not shown
- index
- 1→2
- boundary
- 4 <= 7: true→10 <= 9: false
- overlap
- not shown→[9,10)
- skipped
- not shown→index 3: 11 <= 12 not evaluated
- result
- not shown→false
- index
- 2→not shown
- Recognize it
- Use it when one resource must handle every interval and any temporal overlap makes the schedule invalid.
- Keep true
- Before checking index i, all adjacent boundaries before i are non-overlapping. In start-sorted order, any overlap must include an adjacent pair, so the first failed comparison proves impossibility.
- Reuse it
- Sort to make a global conflict local, then compare neighboring boundaries and stop at the first violation. This transfers to overlap validation, booking conflicts, and disjoint-range checks.
Reference drawing
Problem trace
One overlapping adjacent pair is enough to reject attendance
Execution trace
- max
- 14
- input
- [[7,10],[2,4],[12,14],[9,11]]
- index 0
- [7,10]
- sorted
- not shown→[[2,4],[7,10],[9,11],[12,14]]
- input
- [[7,10],[2,4],[12,14],[9,11]]→not shown
- index 0
- [7,10]→[2,4]
- index
- not shown→1
- boundary
- not shown→4 <= 7: true
- sorted
- [[2,4],[7,10],[9,11],[12,14]]→not shown
- index
- 1→2
- boundary
- 4 <= 7: true→10 <= 9: false
- overlap
- not shown→[9,10)
- skipped
- not shown→index 3: 11 <= 12 not evaluated
- result
- not shown→false
- index
- 2→not shown
- Recognize it
- Use it when one resource must handle every interval and any temporal overlap makes the schedule invalid.
- Keep true
- Before checking index i, all adjacent boundaries before i are non-overlapping. In start-sorted order, any overlap must include an adjacent pair, so the first failed comparison proves impossibility.
- Reuse it
- Sort to make a global conflict local, then compare neighboring boundaries and stop at the first violation. This transfers to overlap validation, booking conflicts, and disjoint-range checks.
Why are neighbors sufficient? Passing adjacent boundaries chain together: every earlier end precedes every later start. A failed comparison exhibits a conflicting pair.
Code
all stops at the first failed comparison.
def can_attend_meetings(intervals: list[list[int]]) -> bool:
intervals.sort()
return all(
intervals[index - 1][1] <= intervals[index][0]
for index in range(1, len(intervals))
)
Cost: comparisons and sorting workspace; the scan needs constant auxiliary state. Integer comparisons count as constant-cost operations.
Change the Requirement
Require Turnaround Time
New task: Require a nonnegative integer turnaround gap without input mutation.
Picture and change: Move each previous availability boundary from end to end + gap.
Code: Retain positive-length intervals; sort a copy.
def can_attend_with_gap(intervals: list[list[int]], gap: int) -> bool:
if gap < 0:
raise ValueError("gap must be nonnegative")
ordered = sorted(intervals)
return all(
ordered[index - 1][1] + gap <= ordered[index][0]
for index in range(1, len(ordered))
)
Check: can_attend_with_gap([[1, 3], [4, 6]], 1) returns True; gap 2 returns False.
Use when: Meetings require travel or reset time. The availability boundaries still chain.
Include Both Endpoints
New task: Include both endpoints.
Picture and change: Shared endpoints conflict; replace <= with <.
Code: Retain positive-length integers; avoid mutation.
def can_attend_closed(intervals: list[list[int]]) -> bool:
ordered = sorted(intervals)
return all(
ordered[index - 1][1] < ordered[index][0]
for index in range(1, len(ordered))
)
Check: can_attend_closed([[1, 3], [3, 5]]) returns False; the original half-open call returns True.
Use when: Occupancy includes both endpoint instants. Costs remain unchanged.
Reconstruct
Why exclude zero-duration rows from the half-open contract?
Reasoned answer
[[1,4], [2,2]] contains an empty half-open interval, yet the adjacent scan returns False.
If empty meetings should occupy no time, filter them before sorting; do not silently reuse the positive-length proof.
Sorting is also necessary: unsorted [[5,6], [1,2]] is compatible despite its descending input order.