https://leetcode.com/problems/meeting-rooms-iii/?envType=daily-question&envId=2025-07-11
oh man i almost got it. I used heap to store the (end_time,room). But my logic for getting the lowest available room value was just through a simple int count variable. Actually we can use ANOTEHR HEAP here and if there indeed is a room in this room heap, we can assign that room to that main heap. Else, we have to wait for the lowest (end_time,room) value stored in main heap.
One impt thing is we have to pop all the values in main heap if their end time is wayyyy before the current iterating start time. Cuz this is to free up the rooms. We push those popped rooms back into rooms heap.
initial
import heapq
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
ans = [0 for _ in range(n)]
heap=[]
heapq.heapify(heap)
meetings.sort(key = lambda x : x[0])
count=0
for event in meetings:
start,end= event[0],event[1]
while heap:
if heap[0][0]<=start:
heapq.heappop(heap)
count=0
else:
break
if len(heap)==n:
duration=end-start
end_time, room = heapq.heappop(heap)
if start>=end_time:
heapq.heappush(heap,(end,room))
else:
heapq.heappush(heap,(end_time+duration,room))
ans[room]+=1
else:
heapq.heappush(heap, (event[1],count))
ans[count]+=1
count+=1
val = -1
real_ans=0
print(ans)
for i in range(n):
if ans[i]>val:
real_ans = i
val=ans[i]
return real_ans
import heapq
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
ans = [0 for _ in range(n)]
heap=[]
heapq.heapify(heap)
meetings.sort(key = lambda x : x[0])
rooms= [i for i in range(n)]
heapq.heapify(rooms)
count=0
for event in meetings:
start,end= event[0],event[1]
while heap and heap[0][0]<=start:
end_timeo,room = heapq.heappop(heap)
heapq.heappush(rooms,room)
if rooms:
room = heapq.heappop(rooms)
heapq.heappush(heap,(end,room))
ans[room]+=1
else:
end_time, room = heapq.heappop(heap)
duration = end-start
ans[room]+=1
heapq.heappush(heap,(end_time+duration,room))
val = -1
real_ans=0
for i in range(n):
if ans[i]>val:
real_ans = i
val=ans[i]
return real_ans
n log n sort
n space