Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],...] (start_i < end_i), find the minimum number of days required to schedule all meetings without any conflicts.
Note: (0,8),(8,10) is not considered a conflict at 8.
Example 1:
Input: intervals = [(0,40),(5,10),(15,20)]
Output: 2Explanation:
day1: (0,40)
day2: (5,10),(15,20)
Example 2:
Input: intervals = [(4,9)]
Output: 1Constraints:
0 <= intervals.length <= 5000 <= intervals[i].start < intervals[i].end <= 1,000,000
You should aim for a solution with O(nlogn) time and O(n) space, where n is the size of the input array.
Try to visualize the meetings as line segments on a number line representing start and end times. The number of rooms required is the maximum number of overlapping meetings at any point on the number line. Can you think of a way to determine this efficiently?
We create two arrays, start and end, containing the start and end times of all meetings, respectively. After sorting both arrays, we use a two-pointer based approach. How do you implement this?
We use two pointers, s and e, for the start and end arrays, respectively. We also maintain a variable count to track the current number of active meetings. At each iteration, we increment s while the start time is less than the current end time and increase count, as these meetings must begin before the earliest ongoing meeting ends.
Then, we increment e and decrement count as a meeting has ended. At each step, we update the result with the maximum value of active meetings stored in count.
Before attempting this problem, you should be comfortable with:
We want to find the minimum number of meeting rooms required so that no meetings overlap.
A useful way to think about this is:
To efficiently track room availability, we use a min heap:
As we process meetings in order of start time:
The maximum size the heap reaches is the number of rooms needed.
min_heap to store meeting end times.min_heap[0]) is less than or equal to the current meeting’s start:"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
intervals.sort(key=lambda x: x.start)
min_heap = []
for interval in intervals:
if min_heap and min_heap[0] <= interval.start:
heapq.heappop(min_heap)
heapq.heappush(min_heap, interval.end)
return len(min_heap)We want to find the minimum number of meeting rooms needed so that no meetings overlap.
Instead of assigning rooms directly, we can look at the problem as a timeline:
At any point in time, the number of rooms required is simply:
the number of meetings happening at that moment
The sweep line technique helps us track how this number changes over time by processing all start and end events in order.
mp to record changes in the number of active meetings:mp[start] (a meeting starts)mp[end] (a meeting ends)prev = 0 to track the number of ongoing meetingsres = 0 to store the maximum number of simultaneous meetingsmp in sorted order:i:prev += mp[i]res = max(res, prev)res."""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
mp = defaultdict(int)
for i in intervals:
mp[i.start] += 1
mp[i.end] -= 1
prev = 0
res = 0
for i in sorted(mp.keys()):
prev += mp[i]
res = max(res, prev)
return resWe want to find the minimum number of meeting rooms required so that no meetings overlap.
Instead of tracking whole intervals, we can separate the problem into two simpler timelines:
If we process these timelines in order:
By moving two pointers over the sorted start and end times, we can track how many meetings are happening at the same time.
The maximum number of simultaneous meetings at any moment is exactly the number of rooms we need.
start → all meeting start times, sortedend → all meeting end times, sorteds = 0 → pointer for start timese = 0 → pointer for end timescount = 0 → current number of ongoing meetingsres = 0 → maximum number of rooms neededs < number of meetings):start[s] < end[e]:count (need one more room)s forwardcount (a room is freed)e forwardres = max(res, count) after each step.res."""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
start = sorted([i.start for i in intervals])
end = sorted([i.end for i in intervals])
res = count = 0
s = e = 0
while s < len(intervals):
if start[s] < end[e]:
s += 1
count += 1
else:
e += 1
count -= 1
res = max(res, count)
return resWe want to find the minimum number of meeting rooms required so that no meetings overlap.
Instead of thinking in terms of rooms directly, we can think in terms of events on a timeline:
So the problem reduces to:
What is the maximum number of meetings happening at the same time?
If we track how the number of active meetings changes over time, the maximum value we ever reach is exactly the number of rooms we need.
This greedy approach works by:
time to store events.(start, +1) → meeting starts (need a room)(end, -1) → meeting ends (free a room)time:-1) are processed before start events (+1) at the same timecount = 0 → current number of ongoing meetingsres = 0 → maximum number of rooms neededtime list:+1 or -1) to countres = max(res, count)res."""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
time = []
for i in intervals:
time.append((i.start, 1))
time.append((i.end, -1))
time.sort(key=lambda x: (x[0], x[1]))
res = count = 0
for t in time:
count += t[1]
res = max(res, count)
return resWhen checking if a room can be reused, the condition should be earliest_end_time <= current_start, not <. A meeting ending at time t allows a room to be reused by a meeting starting at time t.
The min heap and two pointer approaches only work correctly when meetings are sorted by start time. Without sorting, you cannot determine the correct order of room allocation.
The heap size changes throughout processing. You need to track the maximum size the heap reaches during iteration, or simply return the final heap size if you only pop when reusing rooms.