This is the solution about Leetcode - Meeting Rooms.

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.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.

Solution

Analysis

Solution1

  1. Store all start time into one array, sort it.
  2. Store all stop time into one array, sort it.
  3. Use the current start time to compare with the previous stop time, if begin[i]<stop[i-1], then overlap.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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) {
int len = intervals.length;
if(len == 0) return true;
int[] begin = new int[len];
int[] stop = new int[len];
for(int i=0; i<len; i++) {
begin[i] = intervals[i].start;
stop[i] = intervals[i].end;
}
Arrays.sort(begin);
Arrays.sort(stop);
for(int i=1; i<len;i++) {
if(begin[i]<stop[i-1]){
return false;
}
}
return true;
}
}