[LeetCode252]Meeting Rooms

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.

Hide Company Tags Facebook
Hide Tags Sort
Hide Similar Problems (H) Merge Intervals (M) Meeting Rooms II

根據start point sort, 再traverse整個vector, 如果後面一個meeting開始的時間比之前一個meeting結束的時間早(即有重疊)則return false。
記得有好幾道題要寫customize compare function的。

class Solution {
    bool canAttendMeetings(vector<Interval>& intervals) {
        sort(intervals.begin(), intervals.end(), [](const Interval& a, const Interval& b){return a.start < b.start;});
        for (int i = 1;  i<intervals.size(); ++i) {
            if(intervals[i].start < intervals[i-1].end) return false;
        return true;
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.