DSA problem
Meeting Rooms
Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], determine if a single person could attend all the meetings. Two meetings conflict whenever they overlap in time — that is, one starts strictly before another finishes.
Meetings touching at an exact boundary do not conflict: if one ends at 10 and another starts at 10, both can be attended by the same person. This open-closed convention is critical for the comparison used in the solution.
A naive O(n^2) check compares every pair of meetings for overlap; this works but is wasteful. Sorting changes the problem dramatically. Once the intervals are sorted by start time, the only pair that can possibly overlap with meeting i is its immediate predecessor in the sorted order, because anything earlier ended even sooner.
The optimal approach therefore sorts by start time and performs a single linear sweep, returning false the moment any meeting starts before the previous one ends. If the sweep completes, every neighboring pair is compatible, so the answer is true. Sorting dominates the cost at O(n log n) with O(1) extra space beyond the sort.
Examples
intervals = [[0,30],[5,10],[15,20]]falseintervals = [[7,10],[2,4]]trueintervals = [[5,8],[8,10]]trueConstraints
- 0 <= intervals.length <= 10^4
- intervals[i].length == 2
- 0 <= start_i < end_i <= 10^6
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.