1. 程式人生 > >[leetcode]252. Meeting Rooms會議室有衝突嗎

[leetcode]252. Meeting Rooms會議室有衝突嗎

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 }