Meeting Rooms
The key idea
Sort the meetings by start time. Once sorted, two meetings conflict only if the next meeting starts before the previous one ends. So a single left-to-right scan comparing each meeting's start against the running latest end answers the whole question.
Problem
Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], determine if a person could attend all meetings.
A person can attend every meeting only if no two meetings overlap in time. Two meetings overlap when one starts before the other ends. Touching endpoints do not count as overlap: a meeting ending at time t and another starting at time t is allowed.
Return true if the person can attend all meetings, and false otherwise.
Constraints
0 <= intervals.length <= 10^4intervals[i].length == 20 <= start_i < end_i <= 10^6
Examples
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Input: intervals = [[7,10],[2,4]]
Output: true
Complexity
Time: O(n log n) Space: O(1)
See the full solution
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Intervals problems
- Insert IntervalMEDIUM
- Merge IntervalsMEDIUM
- Non-overlapping IntervalsMEDIUM