LeetCode 252. Meeting Rooms

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

Question: 
/*
    Given an arry of meeting time interval consisting of start and end
    times [[s1, e1], [s2, e2],....], determine if a person would attend
    all meetings.
    For example:
        Given [[0, 30], [5, 10], [15, 20]];
        return false
*/

struct Interval{
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};

bool sorter(Interval start, Interval end) {
    return start.start < end.start;
}

bool overlap(Interval i, Interval j) {
    return j.start < i.end;
}

bool canAttend(vector<Interval>& intervals) {
    if(intervals.size() == 0) return true;
    sort(intervals.begin(), intervals.end(), sorter);
    for(int i = 0; i + 1 < intervals.size(); ++i) {
        if(overlap(intervals[i], intervals[i+1])) return false;
    }
    return true;
}


int main(void) {
    Interval v1(0, 3);
    Interval v2(5, 10);
    Interval v3(15, 20);
    vector<Interval> intervals{v1, v2, v3};
    cout << canAttend(intervals) << endl;
}




你可能感兴趣的:(LeetCode 252. Meeting Rooms)