LeetCode Β· #252
Meeting Rooms Solution
Given an array of meeting time intervals, determine if a person can attend all meetings (no overlaps).
01
Section One Β· Problem
Problem Statement
Given intervals where intervals[i] = [startα΅’, endα΅’], return true if a person can attend all meetings (i.e., no two meetings overlap).
Example
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false // [0,30] overlaps with [5,10] and [15,20] Input: intervals = [[7,10],[2,4]]
Output: true
Constraints
β’ 0 β€ intervals.length β€ 10β΄
02
Section Two Β· Approach 1
Brute Force β O(nΒ²)
Compare every pair of intervals. If any pair overlaps, return false.
Problem: O(nΒ²). Sorting by start means you only need to check adjacent pairs β if intervals[i] overlaps intervals[i+1], they'll be adjacent after sorting.
Java β O(nΒ²)
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
for (int i = 0; i < intervals.length; i++)
for (int j = i+1; j < intervals.length; j++)
if (intervals[i][0] < intervals[j][1] && intervals[j][0] < intervals[i][1])
return false;
return true;
}
}
Python β O(nΒ²)
class Solution:
def canAttendMeetings(self, intervals: list[list[int]]) -> bool:
for i in range(len(intervals)):
for j in range(i+1, len(intervals)):
if intervals[i][0] < intervals[j][1] and intervals[j][0] < intervals[i][1]:
return False return True
03
Section Three Β· Approach 2
Sort + Adjacent Check β O(n log n)
Sort by start time. Check if any interval's start is before the previous interval's end β if so, overlap detected.
π‘ Mental model: Line up all meetings on a calendar sorted by start time. Walk left to right: each meeting must start after (or when) the previous one ends. If any meeting starts before the previous ends, there's a conflict. One pass, O(n).
π― When to recognize this pattern: "Can all intervals coexist without overlap?" Sort + check adjacent is the simplest interval problem. It's the gateway to LC 253 (Meeting Rooms II β how many rooms needed).
04
Section Four Β· Trace
Visual Walkthrough
Trace for intervals = [[7,10],[2,4]].
Meeting Rooms β Sort + Check
05
Section Five Β· Implementation
Code β Java & Python
Java β Sort + check
import java.util.*;
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 1; i < intervals.length; i++)
if (intervals[i][0] < intervals[i-1][1])
return false; // overlap: starts before previous ends return true;
}
}
Python β Sort + check
class Solution:
def canAttendMeetings(self, intervals: list[list[int]]) -> bool:
intervals.sort()
return all(intervals[i][0] >= intervals[i-1][1]
for i in range(1, len(intervals)))
06
Section Six Β· Analysis
Complexity Analysis
| Approach | Time | Space |
|---|---|---|
| Brute force | O(nΒ²) | O(1) |
| Sort + check β optimal | O(n log n) | O(1) |
07
Section Seven Β· Edge Cases
Edge Cases & Pitfalls
| Case | Input | Why It Matters |
|---|---|---|
| Empty | [] | No meetings β true. |
| One meeting | [[1,2]] | No conflict possible β true. |
| Touching | [[1,2],[2,3]] | start=2 β₯ end=2 β no overlap β true. |
| Complete overlap | [[1,5],[2,3]] | start=2 < end=5 β overlap β false. |
β Common Mistake: Using
β€ instead of < for the overlap check. Intervals [1,2] and [2,3] touch but don't overlap β the person can attend both. The overlap condition is strict: intervals[i].start < intervals[i-1].end.