-
Notifications
You must be signed in to change notification settings - Fork 9
/
MeetingRooms2.java
39 lines (37 loc) · 1.04 KB
/
MeetingRooms2.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
/**
* 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 int minMeetingRooms(Interval[] intervals) {
List<Integer> times = new ArrayList<>();
for (Interval itv : intervals) {
times.add(itv.start);
times.add(-itv.end);
}
Collections.sort(times, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
if (Math.abs(a) != Math.abs(b))
return Integer.compare(Math.abs(a), Math.abs(b));
else
return Integer.compare(a, b);
}
});
int rooms = 0;
int minRooms = 0;
for (int t : times) {
if (t >= 0) {
rooms++;
minRooms = Math.max(minRooms, rooms);
} else {
rooms--;
}
}
return minRooms;
}
}