<p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>

<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>

<p>Meetings are allocated to rooms in the following manner:</p>

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

<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>

<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1. 
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li>
	<li><code>meetings[i].length == 2</code></li>
	<li><code>0 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 5 * 10<sup>5</sup></code></li>
	<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>

from heapq import heappop, heappush
import bisect
from typing import Dict, List, Tuple


class Solution:
    def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
        meetings_held_by_rooms = [0 for _i in range(0, n)]
        meetings.sort(key=lambda m: m[0])

        free_rooms = [roomIdx for roomIdx in range(0, n)]
        schedule: List[Tuple[int, List[int]]] = []
        schedule_dict: Dict[int, List[int]] = {}

        time = meetings[0][0]

        while meetings:
            if schedule and time == schedule[0][0]:
                t, room_to_free = heappop(schedule)
                for room_idx in room_to_free:
                    bisect.insort(free_rooms, room_idx)
                    meetings_held_by_rooms[room_idx] += 1

                del schedule_dict[time]

            while meetings:
                if not free_rooms:
                    break

                if time < meetings[0][0]:
                    break

                room_idx = free_rooms.pop(0)
                meeting_start, meeting_end = meetings.pop(0)
                duration = meeting_end - meeting_start

                if meeting_start < time:
                    meeting_start = time
                    meeting_end = time + duration

                if meeting_end in schedule_dict:
                    other_rooms_freeing_up_at_same_time = schedule_dict[meeting_end]
                    other_rooms_freeing_up_at_same_time.append(room_idx)
                else:
                    freeing_up_rooms = [room_idx]
                    schedule_dict[meeting_end] = freeing_up_rooms
                    heappush(schedule, (meeting_end, freeing_up_rooms))

            if meetings:
                if free_rooms and schedule:
                    time = min(schedule[0][0], meetings[0][0])
                elif free_rooms and not schedule:
                    time = meetings[0][0]
                else:
                    time = schedule[0][0]

        for _te, rooms in schedule:
            for room_idx in rooms:
                meetings_held_by_rooms[room_idx] += 1

        room_idx_held_max_meetings = 0
        max_meetings_held = meetings_held_by_rooms[0]

        for room_idx, meetings_held in enumerate(meetings_held_by_rooms):
            if meetings_held > max_meetings_held:
                room_idx_held_max_meetings = room_idx
                max_meetings_held = meetings_held

        return room_idx_held_max_meetings

    def _sort_by_meeting_start_time(
        self, m1: Tuple[int, int], m2: Tuple[int, int]
    ) -> int:
        return m1[0] - m2[0]

53200