loading

Meeting Rooms — Step-by-Step Visualization

easyLeetCode #252ArraySorting

Given an array of meeting time intervals where `intervals[i] = [start_i, end_i]`, determine if a person could attend all meetings.

Algorithm Pattern

Sort by Start Time

Key Idea

To determine if any meetings overlap, we first sort the meetings by their start times. Then, we iterate through the sorted meetings and check if the current meeting starts before the previous one ends. If it does, there's an overlap, and the person cannot attend all meetings.

Step-by-Step Approach

  1. Sort intervals by start time: `intervals.sort(key=lambda x: x[0])`.
  2. Iterate from index 1 to `len(intervals) - 1`.
  3. If `intervals[i][0] < intervals[i-1][1]`, return `False`.
  4. Return `True`.

Common Gotchas

  • The sorting step is crucial; without it, we'd have to compare all pairs of meetings (O(n²)).
  • Empty or single meeting cases are always true.

Related Problems