### 题目

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.

### 先排序，$O(n\log_{}{n})$

#### 代码

/**
* Definition for an interval.
* public class Interval {
*     int start;
*     int end;
*     Interval() { start = 0; end = 0; }
*     Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if (intervals.length < 2) { return true; }
Arrays.sort(intervals,new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) {
return i1.start - i2.start;
}
});
for (int i = 1; i < intervals.length; i++) {
if (intervals[i-1].end > intervals[i].start) { return false; }
}
return true;
}
}


### 在比较的时候，发现冲突就抛出异常

#### 代码

public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if (intervals.length < 2) { return true; }
try {
Arrays.sort(intervals,new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) throws RuntimeException {
if (i1.start < i2.start && i1.end <= i2.start) { return -1; }
if (i1.start > i2.start && i1.start >= i2.end) { return 1; }
throw new RuntimeException("Duplicate!");
}
});
} catch (RuntimeException e) {
return false;
}
return true;
}
}


### 最后证明一遍，朴素的 $O(n^2)$的遍历法真的很慢

#### 代码

public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if (intervals.length < 2) { return true; }
for (int i = 1; i < intervals.length; i++) {
for (int j = 0; j < i; j++) {
if (isConflict(intervals[i],intervals[j])) { return false; }
}
}
return true;
}
public boolean isConflict(Interval a, Interval b) {
boolean ibfj = (a.start < b.start) && (a.end <= b.start);
boolean iaftj = (a.start > b.start) && (a.start >= b.end);
return !(ibfj || iaftj);
}
}