lintcode 会议室

lintcode 会议室

描述

给定一系列的会议时间间隔,包括起始和结束时间[[s1,e1],[s2,e2],…(si < ei),确定一个人是否可以参加所有会议。

样例

样例1

输入: intervals = [(0,30),(5,10),(15,20)]
输出: false
解释:
(0,30), (5,10) 和 (0,30),(15,20) 这两对会议会冲突
样例2

输入: intervals = [(5,8),(9,15)]
输出: true
解释:
这两个时间段不会冲突

思路

根据开始时间和结束时间排序,只要前一个会议的结束时间不晚于后一个会议的开始时间就可以参加

代码

/**
 * Definition of Interval:
 * classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param intervals: an array of meeting time intervals
     * @return: if a person could attend all meetings
     */
     
    static bool cmp(Interval a, Interval b) {
        
        return a.start == b.start ? a.end <= b.end : a.start < b.start;
    } 
     
    bool canAttendMeetings(vector<Interval> &intervals) {
        // Write your code here
        if(intervals.size() == 0) return true;
        sort(intervals.begin(), intervals.end(), cmp);
        for (int i = 0; i < intervals.size() - 1; i++) {
            if (intervals[i].end > intervals[i+1].start)
                return false;
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_40147449/article/details/88097660