c++ with sorting by start time


  • 0
    D

    pretty straight forward.

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

Log in to reply
 

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