Hide sidebar

Meeting Rooms

Meeting Rooms

IntervalsArraysSorting
EasyLeetCode #252
15 min

Problem Statement

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

Example

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]

Initial Intervals:

[0, 30]
[5, 10]
[15, 20]

Can Attend All Meetings?

false

Output: false

Solution

The key to this problem is to check for any overlaps. If any two meetings overlap, it's impossible to attend both. Sorting the intervals by their start time makes it easy to check for overlaps.

Algorithm Steps

  • Sort the intervals based on their start times.
  • Iterate through the sorted intervals and check if the start time of the current meeting is before the end time of the previous one.
  • If an overlap is found, return false.
  • If the loop completes without finding any overlaps, return true.
[0, 30]
[5, 10]
[15, 20]
Start with the unsorted intervals.
Meeting Rooms Solution

class Solution:
    def canAttendMeetings(self, intervals: list[list[int]]) -> bool:
        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