clean O(nlogn) c++ solution


  • 0
    W
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     *     Interval() : start(0), end(0) {}
     *     Interval(int s, int e) : start(s), end(e) {}
     * };
     */
    class Solution {
    public:
        
        static bool cmp(const Interval& a, const Interval& b){
            return a.start < b.start;
        }
        
        bool canAttendMeetings(vector<Interval>& intervals) {
            sort(intervals.begin(), intervals.end(), cmp);
            
            for(int i=1; i<intervals.size(); ++i)
                if(intervals[i].start < intervals[i-1].end)
                    return false;
            
            return true;
        }
    };
    

Log in to reply
 

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