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.
一眼見到發現這題好簡單啊, 排序就好了呀, 結果發現只擊敗了6%左右的java代碼,這不能忍受啊。問題一定在排序上。發現把Interval 拆開放在兩個數組里,再排序, 就能擊敗90%多的代碼, 怎么差別那么大, Σ(っ °Д °;)っ
?那有沒有不排序就可以解決的算法呢。沒有完美的, 算了。。。。
public boolean canAttendMeetings(Interval[] intervals) {
? ? ? ? ? ?Arrays.sort(intervals, (a, b)-> a.start - b.start);
? ? ? ? ? ?for(int i = 1 ; i < intervals.length; i++){
? ? ? ? ? ? ? ? ? ? ?if(intervals[i].start < intervals[i-1].end){
? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?return true;
}