920 · Meeting Rooms - LintCode
# Description
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.
(0,8),(8,10) is not conflict at 8
# 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
# Solution
- 用 comparator
- 前者 end time 要比 當前 start time 少
public class Solution { | |
/** | |
* @param intervals: an array of meeting time intervals | |
* @return: if a person could attend all meetings | |
*/ | |
public boolean canAttendMeetings(List<Interval> intervals) { | |
if (intervals == null || intervals.size() == 0) { | |
return true; | |
} | |
Collections.sort(intervals, new Comparator<Interval>(){ | |
public int compare(Interval a, Interval b) { | |
return a.start - b.start; | |
} | |
}); | |
for (int i = 0; i < intervals.size() - 1; i++) { | |
if (intervals.get(i).end > intervals.get(i + 1).start) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |