252. Meeting Rooms
Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.
想法: 將結束時間比較早的先做,所以排序是用 end time。 所以,如果剩下的的會議,其開始時間早於正在執行的會議結束時間,就回傳 false。
1
class Solution {
2
public:
3
bool canAttendMeetings(vector<vector<int>>& intervals) {
4
sort(intervals.begin(), intervals.end(), [](auto const& a, auto const& b) {
5
//sort each meeting by its end time
6
return a[1] < b[1];
7
});
8
for (int i = 1; i < intervals.size(); i++) {
9
//if a meeting ends after the next meeting starts you cannot attend both
10
if (intervals[i][0] < intervals[i - 1][1]){
11
return false;
12
}
13
}
14
return true;
15
}
16
};
Copied!
Copy link