Skip to content

Commit fb2b1ba

Browse files
authored
Create Meeting Rooms III.java
1 parent e84e665 commit fb2b1ba

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Hard/Meeting Rooms III.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int mostBooked(int n, int[][] meetings) {
3+
Arrays.sort(meetings, Comparator.comparingInt(o -> o[0]));
4+
PriorityQueue<int[]> ongoingMeetings = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
5+
int[] frequency = new int[n];
6+
PriorityQueue<long[]> pq = new PriorityQueue<>((o1, o2) -> o1[0] == o2[0] ? (int) (o1[1] - o2[1]) : (int) (o1[0] - o2[0]));
7+
for (int i = 0; i < n; i++) {
8+
pq.add(new long[]{0, i});
9+
}
10+
for (int[] meeting : meetings) {
11+
int startTime = meeting[0];
12+
while (pq.peek()[0] < startTime) {
13+
pq.add(new long[]{startTime, pq.poll()[1]});
14+
}
15+
long[] finishedMeeting = pq.poll();
16+
int room = (int) finishedMeeting[1];
17+
long endTime = finishedMeeting[0] + (meeting[1] - meeting[0]);
18+
frequency[room]++;
19+
pq.add(new long[]{endTime, room});
20+
}
21+
int maxCount = 0;
22+
int roomWithMaxCount = 0;
23+
for (int i = 0; i < n; i++) {
24+
if (frequency[i] > maxCount) {
25+
maxCount = frequency[i];
26+
roomWithMaxCount = i;
27+
}
28+
}
29+
return roomWithMaxCount;
30+
}
31+
}

0 commit comments

Comments
 (0)