10 line c++ solution using lambda function


  • 7
    W
    class Solution {
    public:
        bool canAttendMeetings(vector<Interval>& intervals) {
            sort(intervals.begin(), intervals.end(), 
                [](const Interval& interval1, const Interval& interval2){
                    return interval1.start < interval2.start;
                });
            
            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.