forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0252-meeting-rooms.java
55 lines (46 loc) · 1.32 KB
/
0252-meeting-rooms.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
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) {
int length = intervals.size();
if (intervals.size() == 0 || length == 1) return true;
// Write your code here
int[] start = new int[length];
int[] end = new int[length];
for (int i = 0; i < length; i++) {
start[i] = intervals.get(i).start;
end[i] = intervals.get(i).end;
}
Arrays.sort(start);
Arrays.sort(end);
int j = 0;
while (j + 1 < length) {
if (end[j] > start[j + 1]) return false;
j++;
}
return true;
}
}
public class Solution {
public boolean canAttendMeetings(List<Interval> intervals) {
Collections.sort(intervals, (a, b) -> a.start - b.start);
for (int i = 0; i + 1 < intervals.size(); i++) {
if (intervals.get(i).end > intervals.get(i + 1).start) {
return false;
}
}
return true;
}
}