NeetCode #836LC-252EasyIntervalsBlind 75NC 150NC 250NC Algo100
← Back to All Problems#836 · #252 · Meeting Rooms(会议室)
📌 Problem Statement & Constraints
You are given an array of meeting time intervals
intervals. Determine whether a person could attend all meetings, i.e. no two intervals overlap. Constraints: 0 <= intervals.length <= 10^4, 0 <= intervals[i][0] < intervals[i][1] <= 10^6.💡 Core Algorithmic Approaches
- Sort the intervals by start time.
- Two meetings conflict exactly when the later one starts before the earlier one ends.
- So a single adjacent scan suffices: check
intervals[i][0] < intervals[i-1][1]for each pair. - Because the array is sorted by start, only adjacent pairs can possibly overlap.
💻 Benchmark Python3 Implementation
class Solution:
def canAttendMeetings(self, 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 # conflict
return True⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log n): the sort dominates; the scan is O(n).
💾 Space Complexity
O(1) beyond the sort.
⚠️ Interview Pitfalls & Follow-ups
- Using
<=for the conflict test: a meeting ending at 5 and another starting at 5 do not conflict, so the test is strict<. - Comparing non-adjacent pairs: after sorting by start, adjacency is sufficient.
- Assuming the input is sorted: it is not, so the sort is required.
- Forgetting the empty input: with no intervals the loop is skipped and
trueis returned, which is correct.