# [LeetCode] 252. 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.

Example 1:

Input: [[0,30],[5,10],[15,20]]
Output: false


Example 2:

Input: [[7,10],[2,4]]
Output: true


NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
for (int i = 0; i < intervals.size(); ++i) {
for (int j = i + 1; j < intervals.size(); ++j) {
if ((intervals[i][0] >= intervals[j][0] && intervals[i][0] < intervals[j][1]) || (intervals[j][0] >= intervals[i][0] && intervals[j][0] < intervals[i][1])) return false;
}
}
return true;
}
};

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

Github 同步地址：

https://github.com/grandyang/leetcode/issues/252

Merge Intervals

Meeting Rooms II

https://leetcode.com/problems/meeting-rooms/

https://leetcode.com/problems/meeting-rooms/discuss/67782/C%2B%2B-sort

https://leetcode.com/problems/meeting-rooms/discuss/67786/AC-clean-Java-solution

posted @ 2016-03-04 07:13 Grandyang 阅读(...) 评论(...) 编辑 收藏