Meeting Rooms

Sort by start — if any meeting begins before the previous one ends, the schedule conflicts.

Easy
Time O(n log n) Space O(1)
LeetCode
2 min read
intervals sorting

Problem#

Return true iff a person can attend all given meetings (i.e., no two overlap).

Examples#

  • [[0,30],[5,10],[15,20]]false.
  • [[7,10],[2,4]]true.

Constraints#

  • 0 <= intervals.length <= 10^4.

Approach#

Sort by start. Walk pairwise: if any meeting starts before the previous one ends, conflict.

Solution#

Meeting Rooms
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
for (int i = 1; i < (int)intervals.size(); ++i)
if (intervals[i][0] < intervals[i-1][1]) return false;
return true;
}
};

Editorial#

After sorting by start, consecutive intervals are the only candidates to conflict — non-adjacent intervals are “blocked” by the one between. The strict < matches “touching but not overlapping” being OK.

Complexity#

  • Time: O(n log n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.