DEV Community

tracelit
tracelit

Posted on • Originally published at tracelit.dev

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
Enter fullscreen mode Exit fullscreen mode

Watch It Run

Watch the algorithm run step by step

Watch the algorithm run step by step

Open interactive visualization

Try it yourself: Open TraceLit and step through every line.


Built with TraceLit — the visual algorithm tracer for LeetCode practice.

Top comments (0)