There are n rooms numbered from 0 to n - 1. You are given a 2D integer array meetings where meetings[i] = [start[i], end[i]] means that a meeting will be held during the half-closed time interval [start[i], end[i]). All the values of start[i] are unique.
Meetings are allocated to rooms in the following manner:
Each meeting will take place in the unused room with the lowest number.
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.
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 = [[1,10],[2,10],[3,10],[4,10]]
Output: 0Explanation:
The room with most meetings held and have lowest number is 0.
Example 2:
Input: n = 3, meetings = [[1,20],[2,10],[3,5],[6,8],[4,9]]
Output: 1Explanation:
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1 as it is lowest.
Constraints:
1 <= n <= 1001 <= meetings.length <= 100,000meetings[i].length == 20 <= start[i] < end[i] <= 500,000start[i] are unique.class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
meetings.sort()
rooms = [0] * n # end time of meetings in rooms
meeting_count = [0] * n
for s, e in meetings:
min_room = 0
found = False
for i in range(n):
if rooms[i] <= s:
found = True
meeting_count[i] += 1
rooms[i] = e
break
if rooms[min_room] > rooms[i]:
min_room = i
if found:
continue
meeting_count[min_room] += 1
rooms[min_room] += e - s
return meeting_count.index(max(meeting_count))Where is the number of rooms and is the number of meetings.
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
meetings.sort()
available = [i for i in range(n)] # Min heap for available rooms
used = [] # Min heap for used rooms [(end_time, room_number)]
count = [0] * n # Count of meetings for each room
for start, end in meetings:
while used and used[0][0] <= start:
_, room = heapq.heappop(used)
heapq.heappush(available, room)
if not available:
end_time, room = heapq.heappop(used)
end = end_time + (end - start)
heapq.heappush(available, room)
room = heapq.heappop(available)
heapq.heappush(used, (end, room))
count[room] += 1
return count.index(max(count))Where is the number of rooms and is the number of meetings.
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
meetings.sort()
available = []
count = [0] * n
for i in range(n):
heapq.heappush(available, (0, i))
for start, end in meetings:
while available and available[0][0] < start:
end_time, room = heapq.heappop(available)
heapq.heappush(available, (start, room))
end_time, room = heapq.heappop(available)
heapq.heappush(available, (end_time + (end - start), room))
count[room] += 1
return count.index(max(count))Where is the number of rooms and is the number of meetings.