<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>

import heapq

class Solution:
    def mostBooked(self, num_rooms, meetings):
        num_times = [0] * num_rooms
        heapq.heapify(meetings)

        end_array = []
        available = [room for room in range(num_rooms)]

        print(meetings)
        print(end_array)

        while len(meetings) > 0:
            add_meeting_start, add_meeting_end = meetings[0]
            
            if len(end_array) > 0:
                add_avail_start, avail_room = end_array[0]

                if add_avail_start <= add_meeting_start:
                    heapq.heappop(end_array)
                    heapq.heappush(available, avail_room)
                    continue

            delay = 0
            next_avail = 0
            if len(available) <= 0:
                next_cost, next_room = heapq.heappop(end_array)
                next_avail = next_room
                delay = next_cost - add_meeting_start
            else:
                next_avail = heapq.heappop(available)

            heapq.heappop(meetings)
            heapq.heappush(end_array, (add_meeting_end + delay, next_avail))
            num_times[next_avail] += 1

        max_index = 0
        max_cost = float('-inf')

        print(num_times)

        for i, times in enumerate(num_times):

            if times > max_cost:
                max_cost = times
                max_index = i

        return max_index



# class Solution:
#     def mostBooked(self, num_rooms, meetings):
#         # Create a list of available rooms using indices from 0 to num_rooms-1
#         available_rooms = [room for room in range(num_rooms)]
#         occupied_rooms = []  # Stores rooms that are currently occupied
#         heapify(available_rooms)  # Convert the available_rooms list into a heap
#         booking_counts = [0] * num_rooms  # Initialize a list to keep track of booking counts per room

#         # Sort the meetings in ascending order based on the start time
#         sorted_meetings = sorted(meetings, key=lambda x: x[0])
#         for start_time, end_time in sorted_meetings:
#             # Check if there are any available rooms at the start time of the meeting
#             while occupied_rooms and occupied_rooms[0][0] <= start_time:
#                 # Room becomes available, add it back to the available_rooms heap
#                 end, room = heappop(occupied_rooms)
#                 heappush(available_rooms, room)

#             if available_rooms:
#                 # Assign an available room from the available_rooms heap to the meeting
#                 room = heappop(available_rooms)
#                 heappush(occupied_rooms, [end_time, room])  # Add the meeting to the occupied_rooms heap
#             else:
#                 # All rooms are occupied, find the room with the earliest end time
#                 current_end, room = heappop(occupied_rooms)
#                 new_end = current_end + end_time - start_time  # Update the room's end time
#                 heappush(occupied_rooms, [new_end, room])

#             booking_counts[room] += 1  # Increment the booking count for the assigned room

#         # Find the room with the maximum booking count and return its index
#         max_booking_count = max(booking_counts)
#         most_booked_room = booking_counts.index(max_booking_count)
#         return most_booked_room
54900