-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meeting_Rooms_III.java
51 lines (44 loc) · 1.58 KB
/
Meeting_Rooms_III.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
class Solution {
public int mostBooked(int n, int[][] meetings) {
PriorityQueue<Integer> freeRooms = new PriorityQueue<>();
PriorityQueue<int[]> busyRooms = new PriorityQueue<>((a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
} else {
return a[1] - b[1];
}
});
for (int i = 0; i < n; i++) {
freeRooms.add(i);
}
int[] count = new int[n];
java.util.Arrays.sort(meetings, (a, b) -> a[0] - b[0]);
for (int[] meeting : meetings) {
int start = meeting[0];
int end = meeting[1];
while (!busyRooms.isEmpty() && busyRooms.peek()[0] <= start) {
int[] finishedMeeting = busyRooms.poll();
freeRooms.add(finishedMeeting[1]);
}
if (!freeRooms.isEmpty()) {
int room = freeRooms.poll();
busyRooms.add(new int[]{end, room});
count[room]++;
} else {
int[] ongoingMeeting = busyRooms.poll();
end += ongoingMeeting[0] - start;
busyRooms.add(new int[]{end, ongoingMeeting[1]});
count[ongoingMeeting[1]]++;
}
}
int maxBooked = 0;
int roomWithMaxBookings = 0;
for (int i = 0; i < n; i++) {
if (count[i] > maxBooked) {
maxBooked = count[i];
roomWithMaxBookings = i;
}
}
return roomWithMaxBookings;
}
}