-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-252-Meeting-Rooms.java
53 lines (44 loc) · 1.49 KB
/
LeetCode-252-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
/*
LeetCode: https://leetcode.com/problems/meeting-rooms/
LintCode: Not
JiuZhang: Not
ProgramCreek: Not
http://buttercola.blogspot.com/2015/08/leetcode-meeting-rooms.html
Analysis:
*/
/**
* 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) {
// if(intervals == null || intervals.length <= 1) return true;
// java.util.Arrays.sort(intervals, new IntervalComparator());
// for(int i = 1; i < intervals.length; i++){
// Interval prev = intervals[i - 1];
// Interval curr = intervals[i];
// if(prev.end > curr.start) return false;
// }
// return true;
// }
// private class IntervalComparator implements Comparator<Interval>{
// public int compare(Interval p, Interval q){
// return p.start - q.start;
// }
// }
public boolean canAttendMeetings(int[][] intervals) {
if (intervals == null || intervals.length == 0) return true;
Arrays.sort(intervals, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int e = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
if (e > intervals[i][0]) return false;
e = intervals[i][1];
}
return true;
}
}