forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeetingRooms.java
38 lines (29 loc) · 1.07 KB
/
MeetingRooms.java
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
30
31
32
33
34
35
36
37
38
package com.fishercoder.solutions;
import com.fishercoder.common.classes.Interval;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**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.*/
public class MeetingRooms {
public boolean canAttendMeetings(Interval[] intervals) {
List<Interval> list = new ArrayList();
for(Interval interval : intervals){
list.add(interval);
}
Collections.sort(list, new Comparator<Interval>(){
@Override
public int compare(Interval o1, Interval o2) {
if(o1.start > o2.start) return 1;
else return -1;
}
});
for(int i = 0; i < list.size()-1; i++){
if(list.get(i).end > list.get(i+1).start) return false;
}
return true;
}
}