- Meeting Rooms
中文English
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.
Example
Example1
Input: intervals = [(0,30),(5,10),(15,20)]
Output: false
Explanation:
(0,30), (5,10) and (0,30),(15,20) will conflict
Example2
Input: intervals = [(5,8),(9,15)]
Output: true
Explanation:
Two times will not conflict
解法1:
先排序,然后看是否有重合。
代码如下:
/**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
* }
*/
struct cmp {
bool operator() (Interval & a, Interval & b) {
return a.start < b.start;
}
} compare;
class Solution {
public:
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
bool canAttendMeetings(vector<Interval> &intervals) {
int n = intervals.size();
sort(intervals.begin(), intervals.end(), compare);
for (int i = 0; i < n - 1; ++i) {
if (intervals[i].end > intervals[i + 1].start) return false;
}
return true;
}
};