-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0253-meeting-rooms-ii.java
81 lines (67 loc) · 1.83 KB
/
0253-meeting-rooms-ii.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* 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: the minimum number of conference rooms required
*/
public int minMeetingRooms(List<Interval> intervals) {
if (intervals.isEmpty()) return 0;
Collections.sort(
intervals,
(a, b) -> Integer.compare(a.start, b.start)
);
Queue<Interval> queue = new PriorityQueue<>((a, b) ->
Integer.compare(a.end, b.end)
);
int count = 0;
for (Interval interval : intervals) {
while (
!queue.isEmpty() && interval.start >= queue.peek().end
) queue.poll();
queue.offer(interval);
count = Math.max(count, queue.size());
}
return count;
}
}
// Two pointer approach
public class Solution {
public int minMeetingRooms(List<Interval> intervals) {
if (intervals.size() == 0) {
return 0;
}
int len = intervals.size();
int[] startTime = new int[len];
int[] endTime = new int[len];
for (int i = 0; i < len; i++) {
startTime[i] = intervals.get(i).start;
endTime[i] = intervals.get(i).end;
}
Arrays.sort(startTime);
Arrays.sort(endTime);
int res = 0;
int count = 0;
int s = 0;
int e = 0;
while (s < len) {
if (startTime[s] < endTime[e]) {
s++;
count++;
} else {
e++;
count--;
}
res = Math.max(res, count);
}
return res;
}
}