O(nlogn) c++ solution. Using sort


  • 0
    M
    class Solution {
    public:
        static bool comp(Interval& a, Interval& b){
            return a.start<b.start || (a.start == b.start && a.end<b.end);
        }
        bool canAttendMeetings(vector<Interval>& intervals) {
            if(intervals.size()<=1)
            return true;
            sort(intervals.begin(),intervals.end(),comp);
            for(int i=0;i<intervals.size()-1;i++){
                if(intervals[i].end > intervals[i+1].start)
                    return false;
            }
            return true;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.