|
| 1 | +import heapq |
| 2 | + |
| 3 | +def most_booked(meetings, rooms): |
| 4 | + |
| 5 | + # Count array to track the number of meetings each room holds |
| 6 | + count = [0] * rooms |
| 7 | + |
| 8 | + # Creat min_heaps for available and used rooms |
| 9 | + available = [i for i in range(rooms)] |
| 10 | + used = [] |
| 11 | + |
| 12 | + # Sort the meeting by start time |
| 13 | + meetings.sort() |
| 14 | + |
| 15 | + # Cycle through all meetings by start and end time |
| 16 | + for start_time, end_time in meetings: |
| 17 | + # Free up rooms that aren't used during current start time |
| 18 | + while used and used[0][0] <= start_time: |
| 19 | + ending, room = heapq.heappop(used) |
| 20 | + heapq.heappush(available, room) |
| 21 | + |
| 22 | + # If no rooms are available, delay meetings until one is |
| 23 | + if not available: |
| 24 | + end, room = heapq.heappop(used) |
| 25 | + end_time = end + (end_time - start_time) |
| 26 | + heapq.heappush(available, room) |
| 27 | + |
| 28 | + # Assign meeting to room with lowest number |
| 29 | + room = heapq.heappop(available) |
| 30 | + heapq.heappush(used, (end_time, room)) |
| 31 | + count[room] += 1 |
| 32 | + |
| 33 | + # Return the room that held the most meetings |
| 34 | + return count.index(max(count)) |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +# Time Complexity = O(mlogm + mlogn) |
| 39 | +# Space Complexity = O(n) |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +############################################################### |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +# Driver code |
| 48 | +def main(): |
| 49 | + meetings = [[[0,10],[1,11],[2,12],[3,13],[4,14],[5,15]], |
| 50 | + [[1,20],[2,10],[3,5],[4,9],[6,8]], |
| 51 | + [[1, 2], [0, 10], [2, 3], [3, 4]], |
| 52 | + [[0, 2], [1, 2], [3, 4], [2, 4]], |
| 53 | + [[1, 9], [2, 8], [3, 7], [4, 6], [5, 11]]] |
| 54 | + rooms = [3, 3, 2, 4, 3] |
| 55 | + |
| 56 | + for i in range(len(meetings)): |
| 57 | + print(i+1, '.', '\tMeetings: ', meetings[i], sep='') |
| 58 | + print('\tRooms: ', rooms[i], sep='') |
| 59 | + booked_rooms = most_booked(meetings[i], rooms[i]) |
| 60 | + print('\n\tRoom that held the most meetings: ', booked_rooms) |
| 61 | + print('-' * 100) |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + main() |
| 65 | + |
0 commit comments