imashutosh51

Meeting Rooms III

Feb 8th, 2026
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. '''
  2. We are going to have two bucket 1.Free rooms 2.Used rooms so when we get a meeting while iterating on the meetings array, we will check is there any meeting room available and then we need to take the lesser number meeting room so we can make Free room bucket as min heap which will store the indices of the free rooms.
  3. Now once we got the meeting room, then we will create a tuple ie. (end_time, room_index) where end_time will consider the delay also if it is there.
  4.  
  5. Now suppose while iterating the meetings array, we got a meeting but there is nothing in the free_rooms but there are chances that some meetings would have been already ended before the start of the current_meeting so we need to meetings from the used_rooms which ended in the increasing order(Top one should have the earliest ending) so we should make the used_rooms also as a min heap.we will pop the already over meeting and keep pushing into the free_rooms because we need to smallest index meeting room.
  6.  
  7. The final case, suppose there is no free_room even after removing the already over meetings and now we genuinely don't have any rooms available, then delay comes into the picture so top element of the used_rooms will be already having the earliest ending meeting(which is gonna end after the start time of current_meeting) so we will start the current meeting after that meeting gets over so we can pop the top element of the used_rooms and push the current meeting including the delay as end_time of popped meeting+duration of the current meeting.
  8.  
  9. for the most used room index, we can maintain an array of room count size, the moment you use any room, increase the value of that index
  10. '''
  11.  
  12.  
  13. from heapq import heappush,heappop
  14. class Solution:
  15.     def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
  16.         free_rooms=[i for i in range(n)]
  17.         used_rooms=[]
  18.         meetings.sort()
  19.         ans=[0]*n
  20.  
  21.         for i in range(len(meetings)):
  22.             start=meetings[i][0]
  23.             end=meetings[i][1]
  24.             duration=end-start
  25.  
  26.             while len(used_rooms)>0 and used_rooms[0][0]<= start:
  27.                 end_time,index=heappop(used_rooms) #0th index denoted the meeting end time,and 1 denotes the room no
  28.                 heappush(free_rooms,index)
  29.  
  30.             if free_rooms:
  31.                 index=heappop(free_rooms)
  32.                 ans[index]+=1
  33.                 heappush(used_rooms, (end,index))
  34.             else:
  35.                 end_time,index=heappop(used_rooms)
  36.                 new_end=end_time+ duration
  37.                 ans[index]+=1
  38.                 heappush(used_rooms,(new_end, index))
  39.  
  40.         k=0
  41.         for i in range(len(ans)):
  42.             if ans[i]>ans[k]:
  43.                 k=i
  44.         return k
  45.  
  46.  
  47.        
Advertisement
Add Comment
Please, Sign In to add comment