Solving Leetcode Interviews in Seconds with AI: Meeting Rooms III
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2402" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given an integer n. There are n rooms numbered from 0 to n - 1. You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti 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 = [[0,10],[1,5],[2,7],[3,4]] Output: 0 Explanation: - 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. Example 2: Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]] Output: 1 Explanation: - 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. Constraints: 1 <= n <= 100 1 <= meetings.length <= 105 meetings[i].length == 2 0 <= starti < endi <= 5 * 105 All the values of starti are unique.
Explanation
- Prioritize Meetings: Sort meetings by their start times to ensure meetings are assigned rooms in the correct order.
- Track Room Availability: Use a min-heap to track available rooms and another min-heap to track occupied rooms and their end times. This allows efficient retrieval of the next available room and determination of when rooms become free.
- Count Meetings: Keep track of the number of meetings held in each room to determine the room with the most meetings.
- Runtime Complexity: O(m log n), where m is the number of meetings and n is the number of rooms.
- Storage Complexity: O(n), due to the heaps and the meetings count array.
Code
import heapq
def mostBooked(n: int, meetings: list[list[int]]) -> int:
"""
Finds the room that held the most meetings.
Args:
n: The number of rooms.
meetings: A list of meetings, where each meeting is a list of [start, end].
Returns:
The number of the room that held the most meetings.
"""
meetings.sort() # Sort meetings by start time
available_rooms = list(range(n))
heapq.heapify(available_rooms) # Min-heap for available rooms
occupied_rooms = [] # Min-heap for occupied rooms (end_time, room_number)
room_counts = [0] * n # Count of meetings held in each room
for start, end in meetings:
# Free up rooms that have finished their meetings
while occupied_rooms and occupied_rooms[0][0] <= start:
_, room = heapq.heappop(occupied_rooms)
heapq.heappush(available_rooms, room)
# If no rooms are available, delay the meeting
if not available_rooms:
end_time, room = heapq.heappop(occupied_rooms)
start = end_time
end = end_time + (end - start) # Adjust the end time to reflect the delay
# Assign the meeting to the lowest-numbered available room
room = heapq.heappop(available_rooms)
room_counts[room] += 1
heapq.heappush(occupied_rooms, (end, room))
# Find the room with the most meetings
max_meetings = 0
most_booked_room = 0
for room, count in enumerate(room_counts):
if count > max_meetings:
max_meetings = count
most_booked_room = room
elif count == max_meetings and room < most_booked_room:
most_booked_room = room
return most_booked_room