LeetCode in Kotlin

2402. Meeting Rooms III

Hard

You are given an integer n. There are n rooms numbered from 0 to n - 1.

You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

Meetings are allocated to rooms in the following manner:

  1. Each meeting will take place in the unused room with the lowest number.
  2. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
  3. When a room becomes unused, meetings that have an earlier original start time should be given the room.

Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

A half-closed interval [a, b) is the interval between a and b including a and not including b.

Example 1:

Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]

Output: 0

Explanation:

Both rooms 0 and 1 held 2 meetings, so we return 0.

Example 2:

Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]

Output: 1

Explanation:

Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.

Constraints:

Solution

import java.util.Arrays

class Solution {
    fun mostBooked(n: Int, meetings: Array<IntArray>): Int {
        val counts = IntArray(n)
        val endTimes = LongArray(n)
        Arrays.sort(meetings) { a: IntArray, b: IntArray -> Integer.compare(a[0], b[0]) }
        for (meeting in meetings) {
            val id = findRoomId(endTimes, meeting[0])
            counts[id]++
            endTimes[id] = endTimes[id].coerceAtLeast(meeting[0].toLong()) + meeting[1] - meeting[0]
        }
        var res = 0
        var count: Long = 0
        for (i in 0 until n) {
            if (counts[i] > count) {
                count = counts[i].toLong()
                res = i
            }
        }
        return res
    }

    private fun findRoomId(endTimes: LongArray, start: Int): Int {
        val n = endTimes.size
        // Find the first one
        for (i in 0 until n) {
            if (endTimes[i] <= start) {
                return i
            }
        }
        // Only when non is not delayed, then we find the smallest one
        var id = 0
        var min = Long.MAX_VALUE
        for (i in 0 until n) {
            if (endTimes[i] < min) {
                min = endTimes[i]
                id = i
            }
        }
        return id
    }
}