The idea was pretty simple:first we sort the in the ascending order of and then intervals
start
we check for the overlapping of E Ach pair of neighboring intervals. If they do, then return false
; After we finish all the checks and has not returned false
, just return true
.
Sorting takes time O(nlogn)
and the overlapping checks take time O(n)
, so this idea is time of total O(nlogn)
.
The code is as follows.
1 classSolution {2 Public:3 BOOLCanattendmeetings (vector<interval>&intervals) {4 sort (Intervals.begin (), intervals.end (), compare);5 intn =intervals.size ();6 for(inti =0; I < n-1; i++)7 if(overlap (Intervals[i], intervals[i +1]))8 return false;9 return true;Ten } One Private: A Static BOOLCompare (interval& Interval1, interval&Interval2) { - returnInterval1.start <Interval2.start; - } the BOOLOverlap (interval& interval1, interval&Interval2) { - returnInterval1.end >Interval2.start; - } -};
[Leetcode] Meeting Rooms