252. Meeting Rooms
At a Glance
- Topic: intervals
- Pattern: Merge Intervals (sort + pairwise check)
- Difficulty: Easy
- Companies: Amazon, Google, Facebook, Microsoft, Bloomberg
- Frequency: High
- LeetCode: 252
Problem (one-liner)
Given meeting intervals [start, end], determine whether one person could attend all meetings (no overlaps). Input: intervals. Output: boolean.
Recognition Cues
- “Can attend all” → no overlap between consecutive meetings after sorting by start
- Sort by start time, verify
next.start >= previous.end(strictly>=for touching ok depending on statement—usually adjacent allowed)
Diagram
At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.
Loading diagram…
Approaches
- Check all pairs —
O(n²)time. - Sort + linear scan —
O(n log n)time /O(1)extra. <- pick this in interview.
Optimal Solution
Go
package main
import "sort"
func canAttendMeetings(intervals [][]int) bool {
if len(intervals) <= 1 {
return true
}
sort.Slice(intervals, func(left int, right int) bool {
return intervals[left][0] < intervals[right][0]
})
for index := 1; index < len(intervals); index++ {
previousEnd := intervals[index-1][1]
currentStart := intervals[index][0]
if currentStart < previousEnd {
return false
}
}
return true
}JavaScript
function canAttendMeetings(intervals) {
if (intervals.length <= 1) return true;
const sorted = [...intervals].sort((left, right) => left[0] - right[0]);
for (let index = 1; index < sorted.length; index++) {
const previousEnd = sorted[index - 1][1];
const currentStart = sorted[index][0];
if (currentStart < previousEnd) return false;
}
return true;
}Walkthrough
[[0,30],[5,10],[15,20]] → after sort by start, overlap between [0,30] and [5,10] → false.
Invariant: sorted by start, only adjacent checks matter for conflicts.
Edge Cases
- Empty list →
true - Touching meetings
[1,2],[2,3]→ typicallytruewith<test
Pitfalls
- Using
<=boundary wrong for inclusive/exclusive end conventions
Similar Problems
- 253. Meeting Rooms II — how many rooms when overlaps exist.
- 435. Non-overlapping Intervals — minimize removals.
Variants
- Minimum rooms → Meeting Rooms II.
Mind-Map Tags
#intervals #sort #overlap-check #easy
Last updated on
Spotted something unclear or wrong on this page?