C++ 4 lines concise solution using lambda


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

    Another solution inspired by this post.

    class Solution {
    public:
        bool canAttendMeetings(vector<Interval>& intervals) {
            map<int, int>m;
            for(auto x: intervals) m[x.start]++, m[x.end]--;
            int sum = 0;
            for(auto x: m) if((sum += x.second) > 1) return false;
            return true;
        }
    };
    

Log in to reply
 

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