Skip to content

Commit 999bb5b

Browse files
authored
Create 2402-meeting-rooms-iii.kt
1 parent 03eac5d commit 999bb5b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

kotlin/2402-meeting-rooms-iii.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
fun mostBooked(n: Int, meetings: Array<IntArray>): Int {
3+
meetings.sortBy { it[0] }
4+
5+
val available = PriorityQueue<Int> ().apply {
6+
for (i in 0 until n)
7+
add(i)
8+
}
9+
10+
val used = PriorityQueue<Pair<Int, Int>> { a, b ->
11+
if (a.first == b.first) a.second - b.second
12+
else a.first - b.first
13+
}
14+
15+
val count = IntArray (n)
16+
17+
for (meet in meetings) {
18+
var (start, end) = meet
19+
while (used.isNotEmpty() && start >= used.peek().first) {
20+
val (_, room) = used.poll()
21+
available.add(room)
22+
}
23+
24+
if (available.isEmpty()) {
25+
val (endTime, room) = used.poll()
26+
end = endTime + (end - start)
27+
available.add(room)
28+
}
29+
30+
val room = available.poll()
31+
used.add(end to room)
32+
count[room]++
33+
}
34+
35+
return count.indexOfFirst({ it == (count.max() ?: 0) })
36+
}
37+
}

0 commit comments

Comments
 (0)