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.
[[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.
false
trueConstraints
0 ≤ intervals.length ≤ 10⁴intervals[i].length == 20 ≤ 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.
Before sort: [0,30] [5,10] [15,20]
After sort: [0,30] [5,10] [15,20]
└─────┘
5 < 30 → conflict detected immediatelySolution
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 Truefunction canAttendMeetings(intervals: number[][]): boolean {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false;
}
}
return true;
}Complexity
| Metric | Value | Reason |
|---|---|---|
| Time | O(n log n) | Dominated by sorting; the linear scan is O(n) |
| Space | O(1) | In-place sort, no extra structure |
Edge Case Thinking
| Scenario | Handling |
|---|---|
| 0 or 1 meeting | Always 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 input | Loop runs, all comparisons pass, return true. |
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.