253. Meeting Rooms II
Given an array of meeting time intervals
intervals
where intervals[i] = [starti, endi]
, return the minimum number of conference rooms required.def minMeetingRooms(intervals: List[List[int]]):
if not intervals:
return 0
usedRooms = 0
# Separate out the start and the end timings and sort them individually.
starts = sorted([i[0] for i in intervals])
ends = sorted(i[1] for i in intervals)
L = len(intervals)
start, end = 0, 0
while start < L:
# If there is a meeting that has ended by the time the meeting at `start` starts
if starts[start] >= ends[end]:
usedRooms -= 1
end += 1
# We do this irrespective of whether a room frees up or not.
# If a room got free, then this used_rooms += 1 wouldn't have any effect.
# used_rooms would remain the same in that case.
# If no room was free, then this would increase used_rooms
usedRooms += 1
start += 1
return usedRooms
def minMeetingRooms(intervals: List[List[int]]):
if not intervals:
return 0
usedRooms = 0
minRooms = 0
starts = [(i[0], 1) for i in intervals]
ends = [(i[1], -1) for i in intervals]
events = sorted(starts + ends)
for _, event in events:
usedRooms += event
minRooms = max(minRooms, usedRooms)
return minRooms
Comments
Post a Comment