Welcome to CodeYourCraft's deep dive into Data Structures and Algorithms! Today, we'll tackle two exciting problems: Meeting Rooms I and Meeting Rooms II. These problems are great practice for understanding the importance of data structures and algorithms in real-world scenarios. Let's get started! šÆ
Imagine you're scheduling meetings for a company. Multiple meetings might require the same room at different times. Today, we'll learn how to find the minimum number of rooms needed to accommodate all meetings without any conflicts.
Given an array of meeting time intervals consisting of start and end times, return the minimum number of rooms required.
For example, if the input is:
[[0, 30], [5, 10], [15, 20]]
The output should be:
2
Since we can have two concurrent meetings from 0 to 10 and 15 to 20 in two separate rooms.
To solve this problem, we can sort the meeting intervals and maintain a counter for the number of active meetings at any given time. For each meeting, if it overlaps with an active meeting, we increment the counter. Otherwise, we decrement the counter.
Here's the Python code for Meeting Rooms I:
def minMeetingRooms(intervals):
# Sort the intervals
intervals.sort(key=lambda x: x[0])
meetings = [] # To store the active meetings
# For each interval, check if it overlaps with an active meeting
for meeting in intervals:
# If the meeting overlaps with an active meeting, increment the counter
if not meetings or meetings[-1][1] < meeting[0]:
meetings.append(meeting)
# If the meeting overlaps with an existing meeting, update the end time of the earlier one
else:
meetings[-1][1] = max(meetings[-1][1], meeting[1])
# The number of meetings (rooms) is the length of the meetings list
return len(meetings)Instead of sorting, you can use a min-heap to efficiently handle the active meetings. This would reduce the time complexity to O(n log k), where k is the maximum number of concurrent meetings.
Now, let's make things a bit more interesting! In Meeting Rooms II, we'll handle a scenario where some meetings can be joined (i.e., a meeting can start before another one ends). We'll find the minimum number of rooms required to accommodate all meetings, even if they can overlap.
Given an array of meeting time intervals consisting of start and end times, return the minimum number of rooms required.
For example, if the input is:
[[0, 30], [5, 10], [15, 20]]
The output should be:
1
Since we can accommodate all meetings in a single room with the following schedule:
To solve this problem, we'll maintain a heap (min-heap for non-overlapping meetings and max-heap for overlapping meetings) to keep track of the meetings that can start at any given time.
Here's the Python code for Meeting Rooms II:
def minMeetingRooms(intervals):
# Create a max-heap to store the meetings that can start at any given time
meetings = [-interval[1] for interval in intervals]
heapq.heapify(meetings)
rooms = 0 # To store the number of active rooms
for meeting in intervals:
# Pop meetings that end after the current meeting's start time
while meetings and meetings[0] <= meeting[0]:
meetings.pop(0)
# If there are meetings that can start at the current meeting's start time, add a room
if meetings and meetings[0] >= meeting[1]:
rooms += 1
# Add the current meeting to the max-heap
heapq.heappush(meetings, -meeting[1])
# The number of active rooms is the final answer
return roomsIf you find the max-heap implementation confusing, you can use two min-heaps: one for meetings that can start at any given time and another for meetings that can end at any given time.
In Meeting Rooms I, how do we handle overlapping meetings?
And that's a wrap for today! You've learned how to schedule meetings in two different scenarios using the Meeting Rooms I and Meeting Rooms II problems. These problems are great practice for understanding data structures and algorithms in real-world scenarios.
Keep practicing, and happy coding! š»šš