252. Meeting Rooms - Detailed Explanation
Problem Statement
You are given an array of meeting time intervals where each interval is represented by a pair of integers ([start, end]). Each interval denotes the start time and end time of a meeting. The task is to determine if a person can attend all the meetings. In other words, the goal is to check whether any two meetings overlap. If there is an overlap, it means that one cannot attend all meetings, and the answer should be false; otherwise, return true.
For example, given intervals = ([[s_1, e_1], [s_2, e_2], dots, [s_n, e_n]]), return true if for every pair of consecutive meetings after sorting by start time, the end time of the previous meeting is less than or equal to the start time of the next meeting.
Hints
-
Sorting by Start Time:
Sorting the meeting intervals by their start times makes it easier to compare consecutive meetings to check for overlaps. -
Checking for Overlap:
Once sorted, iterate through the list of meetings and check whether the end time of a meeting is greater than the start time of the next meeting. If it is, there is an overlap. -
Edge Cases:
Consider scenarios such as:- An empty list of meetings.
- A list with only one meeting.
- Meetings that touch but do not overlap (e.g., one meeting ends exactly when the next one starts).
Approaches
Approach 1: Sorting and Comparing
-
Sort the Intervals:
Sort the intervals based on their start times. If two meetings have the same start time, you can further sort by end time. -
Iterate Through the Sorted List:
After sorting, iterate from the first meeting to the second-to-last meeting and check whether the end time of the current meeting is greater than the start time of the next meeting.- If yes, return false because the meetings overlap.
- If no overlap is found for any consecutive meetings, return true.
Complexity Analysis
-
Time Complexity:
Sorting takes (O(n \log n)) and iterating through the sorted list takes (O(n)), so the overall time complexity is (O(n \log n)). -
Space Complexity:
Sorting may take additional space depending on the sorting algorithm, but overall the space complexity is (O(1)) (ignoring the space required for the input).
Python Code
Java Code
Step-by-Step Walkthrough
-
Sorting the Intervals:
Given an array of intervals, sort them based on their start time.- Example: For intervals ([[0, 30], [5, 10], [15, 20]]), sorting by start time results in the same order: ([[0, 30], [5, 10], [15, 20]]).
-
Checking for Overlaps:
Iterate through the sorted list:- Compare the end time of the first meeting (30) with the start time of the second meeting (5). Since 30 > 5, there is an overlap.
- As soon as an overlap is detected, return false.
-
Return Result:
If no overlaps are found after checking all consecutive pairs, return true.
Common Mistakes
-
Not Sorting the Intervals:
Without sorting, you might incorrectly conclude that meetings do not overlap because the intervals are not in order. -
Incorrect Overlap Condition:
Be careful with the condition. Meetings that "touch" (e.g., one ends at the exact time the next starts) do not overlap.
Edge Cases
-
Empty Array:
If there are no meetings, the function should return true because there is nothing to conflict. -
Single Meeting:
With only one meeting, there is no possibility of overlap, so the result should be true. -
Touching Meetings:
Meetings where the end time of one is exactly equal to the start time of another (e.g., ([[1,2],[2,3]])) should be considered non-overlapping.
Related Problems
GET YOUR FREE
Coding Questions Catalog