Meeting Rooms
Problem
Solution
/**
* @param {number[][]} intervals
* @return {boolean}
*/
var canAttendMeetings = function(intervals) {
const sorted = intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < sorted.length; i++) {
const endPrev = sorted[i - 1][1];
const startCur = sorted[i][0];
if (endPrev > startCur) {
return false;
}
}
return true;
};
We will implement a greedy solution. Observe that all meetings can be attended if and only if there are no overlaps. This can be done by sorting intervals
by the start time and making sure each meeting ends before the next one starts.