Meeting Rooms
The drill: A single person's day is booked as a list of meetings. Decide whether they can attend every one of them — that is, whether any two ever overlap.
One person's schedule for a day arrives as a list of meetings, each with a start and an end time. The job is to say whether that single person could physically sit through every meeting on the list.
Attending everything is possible only when no two meetings ever overlap in time — a meeting ending at the exact moment another begins is fine, since nothing actually collides.
The answer is a single yes-or-no: true if the whole day can be attended start to finish, false the moment any pair of meetings genuinely conflicts.
- up to a few thousand meetings in one call
- start and end times are non-negative integers
- a meeting's end is never earlier than its own start
- a meeting ending exactly when another starts is not a conflict
HINT 1 THE NUDGE
Two meetings conflict only if one starts before the other ends. Checking every pair works, but a cheap reordering turns 'any two might conflict' into 'only neighbors can conflict'.
HINT 2 THE STRUCTURE
Sort the meetings by start time. After sorting, any conflict must show up between two meetings that sit next to each other in that order.
HINT 3 ONE STEP FROM THE ANSWER
Sort by start, then walk the list once: if any meeting's start time is less than the previous meeting's end time, the day is impossible — return false. If the walk finishes clean, return true.
Meetings [5,10], [0,3], [3,6]. Sort by start first — a conflict can only ever hide between neighbors: [0,3], [3,6], [5,10].
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
ivs = sorted(intervals, key=lambda iv: iv[0])
for i in range(1, len(ivs)):
if ivs[i][0] < ivs[i - 1][1]:
return False
return Trueclass Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
n = len(intervals)
for i in range(n):
for j in range(i + 1, n):
a, b = intervals[i], intervals[j]
if a[0] < b[1] and b[0] < a[1]:
return False
return Trueclass Solution {
public boolean canAttendMeetings(int[][] intervals) {
int[][] ivs = intervals.clone();
Arrays.sort(ivs, (a, b) -> Integer.compare(a[0], b[0]));
for (int i = 1; i < ivs.length; i++) {
if (ivs[i][0] < ivs[i - 1][1]) {
return false;
}
}
return true;
}
}class Solution {
public boolean canAttendMeetings(int[][] intervals) {
for (int i = 0; i < intervals.length; i++) {
for (int j = i + 1; j < intervals.length; j++) {
int[] a = intervals[i], b = intervals[j];
if (a[0] < b[1] && b[0] < a[1]) {
return false;
}
}
}
return true;
}
}✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED