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
题目
给定一些区间,判断是否有重合。
还是挺实际的场景,经常在图书馆预定study room的系统,内部也应该是这个逻辑。 当有overlapping的时候,就会报错。
思路
任何一组intervals, 若当前start < 之前end,即出现了overlapping
代码
1 class Solution { 2 public boolean canAttendMeetings(Interval[] intervals) { 3 if(intervals == null || intervals.length==0) return true; 4 int []start = new int[intervals.length]; 5 int []end = new int[intervals.length]; 6 for(int i = 0; i<intervals.length;i++){ 7 start[i] = intervals[i].start; 8 end[i] = intervals[i].end; 9 } 10 Arrays.sort(start); 11 Arrays.sort(end); 12 /* 任何一组intervals, 若当前start < 之前end,即出现了overlapping 13 | i-1 | 14 | i | 15 */ 16 for(int i = 1; i< start.length; i++){ 17 if(start[i]<end[i-1]) return false; 18 } 19 return true; 20 } 21 }