LeetCode 252: Meeting Rooms — Step-by-Step Visual Trace
Easy — Array | Sorting | Intervals | Greedy
The Problem
Given an array of meeting time intervals, determine if a person could attend all meetings without any conflicts. Return false if any two meetings overlap in time.
Approach
Sort the intervals by start time, then iterate through consecutive meetings to check if any meeting starts before the previous one ends. If such an overlap is found, return false; otherwise, return true.
Time: O(n log n) · Space: O(1)
Code
class Solution:
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
if not intervals:
return True
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 True
Watch It Run
Try it yourself: Open TraceLit and step through every line.
Built with TraceLit — the visual algorithm tracer for LeetCode practice.
Comments