252. Meeting Rooms π
Description
You are given an array of meeting times intervals where intervals[i] = [starti, endi].
A person can attend all meetings if no two meeting intervals overlap. Meetings ending at time t and starting at time t do not overlap.
βββββββReturn true if a person can attend all meetings. Otherwise, return false.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]] Output: false
Example 2:
Input: intervals = [[7,10],[2,4]] Output: true
Constraints:
0 <= intervals.length <= 104intervals[i].length == 20 <= starti < endi <= 106
Solutions
Solution 1: Sorting
Thinking
One person cannot attend two overlapping meetings. After sorting by start time, it is enough that each meeting ends no later than the next one starts.
We sort the meetings based on their start times, and then iterate through the sorted meetings. If the start time of the current meeting is less than the end time of the previous meeting, it indicates that there is an overlap between the two meetings, and we return false. Otherwise, we continue iterating.
If no overlap is found by the end of the iteration, we return true.
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(\log n)\), where \(n\) is the number of meetings.
1 2 3 4 | |
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 | |